|
Dominoes posted:Is there a trick to getting matplotlib plots to not freeze the GUI? For tkinter, the trick is to use plt.save(), then load that image onto a Canvas object. Don't use .show().
|
# ? Nov 21, 2013 23:10 |
|
|
# ? Jun 13, 2024 05:52 |
|
You're close. First of all, a property must return something. It should be thought of as closer to an attribute than a method. Python code:
Python code:
SurgicalOntologist fucked around with this message at 00:19 on Nov 22, 2013 |
# ? Nov 22, 2013 00:15 |
|
I've always rolled with the default python installations. However, I did have to spend quite a bit of time learning and fighting with easy_install, pip and compiler nonsense. It was instructive, but I'm not sure if I would recommend it if it could be avoided.Dominoes posted:.... Both of your examples will throw attribute exceptions, because my_instance.hi doesn't get set until my_method gets called (with or without the property). Properties are most useful as a way of tucking away calculations based on other members of an object. A trivial example: Python code:
Haystack fucked around with this message at 00:41 on Nov 22, 2013 |
# ? Nov 22, 2013 00:38 |
|
Dominoes posted:What's the deal with @property? PyCharm suggests adding this decorator to methods that have no arguments other than 'self'. As a result, the method names are then called as class variables instead of methods, ie without the (). So, it's a way of saying "this is a class variable that changes automatically". Okay, so first: properties and getters and setters. As you may know, in Java, you have class properties, like a "public double width;". But you should never access those from outside the class, because what if you suddenly wanted to calculate the width as "area / height"? Or what if you wanted to make sure that width is always positive?! What if??! So instead you make a "private double width;", and a "public double getWidth()" and a "public void setWidth(double width)", and those methods are your getters/setters. And you probably have an IDE that automatically generates getters and setters, because 99% of the time they're just boilerplate around a private property. For a read-only property, you may have a "public double getArea()" but not a corresponding "setArea()". Now writing all those setter and getter methods is unpythonic, because YAGNI. So in Python, you just assume that everything that looks like a property is a property. So instead of myrectangle.setWidth(100), you'll write myrectangle.width = 100. But now What If you wanted to change the internals of Rectangle to store the height and area instead? Well, in Python, you can give Rectangle a 'width' property to define what myrectangle.width = 100 really means. So you'll write Python code:
Or you could write Python code:
So now the onus on you, the developer, to determine what should be a "method" and what should be a "property". Obviously .doComplicatedStuff(a, b, c) should be a method, and .width should probably be a property, but what about the stuff in between? It's not an exact science, but generally a 'property' should be cheap to access (O(1) at least), reading it should have no side effects, and it should not change unexpectedly. If it takes arguments, if it can take some time to calculate, or if the result can change for reasons outside the control of the programmer, it should probably be a method. Most importantly, don't write code before it's needed. If it feels like the width of a rectangle should be an attribute you access directly with "myrectangle.width", do that, and if it turns out that you need to check that width is always positive, add a setter later using @property. The point of @property is first and foremost to avoid writing a million get*()/set*() methods you don't really need.
|
# ? Nov 22, 2013 00:54 |
|
Thermopyle posted:One of you is saying on the new OP document to not suggest Windows users download the python.org Windows version but to instead download a Windows-specific distribution like...well like, I don't know what. But anyway... My thoughts as a total programming and Python newbee is that I would rather download the official package for the OS I'm working on, and learn the ins and outs via a text editor and then passing it through an interpreter like Powershell. Granted this is all I know based on LPTHW but it makes total sense to me. All the other things I can dabble in and gain a preference for AFTER I've gotten a good foundation and basic hold on concepts and the language.
|
# ? Nov 22, 2013 02:28 |
|
Thanks for the info on properties; Going through my code to see where they're appropriate.Dominoes posted:I'm also looking for info on changing the plot window size (without having to adjust it with the mouse after it opens). The guides show using figure() objects, but I'm trying to use a plot. Python code:
Dominoes fucked around with this message at 03:07 on Nov 22, 2013 |
# ? Nov 22, 2013 03:04 |
|
Thermopyle posted:One of you is saying on the new OP document to not suggest Windows users download the python.org Windows version but to instead download a Windows-specific distribution like...well like, I don't know what. But anyway... It wasn't me, but I agree with the comment. I'd suggest Anaconda or Pythonxy (they're both pretty similar) over building your own environment. It's going to significantly easier to install and use those than to install all of the individual libraries that you might need, plus they come with nice IDEs for you to develop in. You are getting the official python.org distribution, but you're also getting a whole lot more. The IDEs alone make this a great option. Dominoes posted:I prefer the official version on Windows by default, from the perspective of a beginner with no interest in scientific computing, and later, as someone who uses it. The target isn't just scientists, it's for everyone who doesn't want to have to gently caress around with installing and customizing their Python environment on an OS that isn't designed for that kind of thing. If you haven't done it before, setting up Python on Windows is a pain in the rear end. These all-in-one packages streamline the process and let you immediately start writing code and seeing results without having to mess with environmental variables or any other installations, even. Scientific libraries are a tiny piece of the pie that you get from Anaconda/Pythonxy/others. They come with extra GUI libraries, web development libraries, miscellaneous API libraries, scientific libraries, etc. They also come with some very nice IDEs that you might never get to see otherwise (a new user might end up using Notepad and the Windows command line or something, which would be horrible by comparison). Some people might prefer the piecemeal build-your-own of going through python.org and then downloading and installing all of the libraries that you need, but for most people that's not going to be the best option in a Windows environment.
|
# ? Nov 22, 2013 04:27 |
|
QuarkJets posted:If you haven't done it before, setting up Python on Windows is a pain in the rear end. These all-in-one packages streamline the process and let you immediately start writing code and seeing results without having to mess with environmental variables or any other installations, even. I'm not convinced. I don't like the idea of having all those libraries installed. It just rubs me the wrong way. The idea of packaged IDE's turns me off of them even more. I always try to default to what is going to have the most support with my Googlin' and these specialized distributions ring my alarm bells in that regard. Now that I've said that, I moved to linux for my development purposes a long time ago because library management was much easier...but my understanding is that it's not too bad on Windows any more. I'll make mention of their availability in the OP alongside python.org. Actually, as I'm typing, I realized that I might mention in the OP what I'm doing nowadays. I switched back to Windows for My Gamez, but pretty much always have Ubuntu running via VMWare on two of my three monitors for development. It's pretty much seamless.
|
# ? Nov 22, 2013 05:59 |
|
edit: nvm
Dominoes fucked around with this message at 08:10 on Nov 22, 2013 |
# ? Nov 22, 2013 08:08 |
|
Thermopyle posted:I'm not convinced. If you use Anaconda you are free to create as many environments with nothing but python as you like: "conda create -n mybarrenenv python=2.7" No other packages. On the other hand, lots of other packages are easily installable, from us, from PyPI, from binstar, if you think mucking with C extension builds on Windows is a crappy hobby (it is). BigRedDot fucked around with this message at 08:28 on Nov 22, 2013 |
# ? Nov 22, 2013 08:24 |
|
Or you could just get python from the main site and run IDLE vv I agree with Thermopyle by far, beginners don't need an IDE at all, especially not when you're starting out coding for the first time ever. If you're going from something else like C++ to python, they also don't need a big package of stuff either because they won't be new to the concept of finding an IDE/modules.
|
# ? Nov 22, 2013 08:35 |
|
Thermopyle posted:I'm not convinced. Why? It's a single self-contained package as far as Windows is concerned, and you're free to remove it whenever you want. They're also self-contained Python package managers, so you can add or remove whatever modules you want without increasing the complexity of your environment. And I also think that being given a nice IDE with a console is a huge quality-of-life improvement for new Python users. That's probably why even the python.org installation comes with one, I just don't think that it's very good. What would you suggest using instead of an IDE, Notepad++ and the Windows command line or something? e: I also don't understand what you mean by "specialized distribution". It's not like they've changed any of the modules, they're just bundling a bunch of things together. Googling for answers is going to be the same regardless QuarkJets fucked around with this message at 11:12 on Nov 22, 2013 |
# ? Nov 22, 2013 10:58 |
|
When I suggest python to windows people I suggest they get anaconda. It is much easier than setting up the python.org version. Oh yeah, and windows people love IDEs. Dominoes, don't feel like you need to go run out and add @property to everything. @property is analogous to getter/setters in java and I think the main reason it is in python is to make java people feel comfortable. In python (and even in java) it is perfectly fine to leave member variables exposed for access by the user if that is what you need to do. Heck, in python the user can always get to them if they want. If you later decide you want to lazy init that value you can make a getter for it with @property. Otherwise, don't bother with @property it is usually extra cruft.
|
# ? Nov 22, 2013 14:09 |
|
QuarkJets posted:And I also think that being given a nice IDE with a console is a huge quality-of-life improvement for new Python users. That's probably why even the python.org installation comes with one, I just don't think that it's very good. What would you suggest using instead of an IDE, Notepad++ and the Windows command line or something? Dren posted:Dominoes, don't feel like you need to go run out and add @property to everything. @property is analogous to getter/setters in java and I think the main reason it is in python is to make java people feel comfortable. In python (and even in java) it is perfectly fine to leave member variables exposed for access by the user if that is what you need to do. Heck, in python the user can always get to them if they want. If you later decide you want to lazy init that value you can make a getter for it with @property. Otherwise, don't bother with @property it is usually extra cruft. Python code:
Dominoes fucked around with this message at 16:32 on Nov 22, 2013 |
# ? Nov 22, 2013 16:14 |
|
Definitely never use @property for something that sends a HTTP request.
|
# ? Nov 22, 2013 16:51 |
|
Thermopyle posted:One of you is saying on the new OP document to not suggest Windows users download the python.org Windows version but to instead download a Windows-specific distribution like...well like, I don't know what. But anyway... I'm a non-scientific user and the last two times I've set up Python on a Windows machine I used Anaconda because basically. I didn't even know it had an IDE involved - I just didn't want to gently caress with getting all those packages individually, which is annoying on Windows because you can't just type sudo apt-get install some-library like you can on *nix.
|
# ? Nov 22, 2013 17:12 |
|
Dominoes posted:Would it be appropriate for cases like this? You can look at stock.last() as a property if you really want to. In terms of functionality I see no reason for it to be a property or a method. However, I would leave it as a method because I expect members without parentheses in python to be instances of objects, not function calls with side-effects like an HTTP request. suffix sarcastically laid out the case for getter/setters in java in his post: suffix posted:Okay, so first: properties and getters and setters. As you may know, in Java, you have class properties, like a "public double width;". But you should never access those from outside the class, because what if you suddenly wanted to calculate the width as "area / height"? Or what if you wanted to make sure that width is always positive?! What if??! The case for use of getter/setters in Java is that you might need to do something beyond simply returning the underlying member and if you ever have to switch to that mode you'll have to refactor all of your code to be a function call instead of referring to an actual object/primitive. So to avoid the potential future refactoring, someone recommended that objects in java get a poo poo ton of boilerplate getter/setter code and everyone listened to that guy. @property in python allows for neatly skirting the issue of refactoring if there is ever a need to do more than simple object access. Switch to a function, decorate with @property, and user code is none the wiser. Imo, this is the only legitimate use-case for @property. But even in this case I would rather switch to using a function and force users to refactor than use @property.
|
# ? Nov 22, 2013 17:22 |
|
Dren posted:You can look at stock.last() as a property if you really want to. In terms of functionality I see no reason for it to be a property or a method. However, I would leave it as a method because I expect members without parentheses in python to be instances of objects, not function calls with side-effects like an HTTP request.
|
# ? Nov 22, 2013 17:53 |
|
Misogynist posted:The danger is in the expectation that property access is instantaneous, and doesn't require any expensive computation. Properties are fine when this attribute is preserved, but things get messy if an end-user of the library expects an immediate result and end up with some O(n) monstrosity crawling their data structure behind the scenes. This is another fine reason to not use properties. Dominoes, I forgot to answer your question about matplotlib figures by pointing you at the matplotlib user's guide. Reading the "Artist tutorial" section will tell you what you need to know about how figures and axes relate. If you need to do more complicated layout stuff, especially stuff with the various coordinate systems, keep the user's guide handy and at least skim most of it. The matplotlib API docs are ok but the user's guide ties together the parts of the API that would otherwise seem disparate.
|
# ? Nov 22, 2013 18:23 |
|
Is there a pypy installer for windows? Or even a chocolaty package
|
# ? Nov 22, 2013 19:09 |
|
Dren posted:In terms of functionality I see no reason for it to be a property or a method. What should it be if not one or the other?
|
# ? Nov 22, 2013 19:40 |
|
breaks posted:What should it be if not one or the other? I meant one over the other.
|
# ? Nov 22, 2013 19:44 |
|
I'm curious about this distributions-vs-python.org thing, so I'm in the middle of spinning up some VMs to test poo poo out. I haven't done any Python on Windows in a long time, and I've barely touched any of the Windows-specific distributions, so I'm going to get familiar with all the options here...
|
# ? Nov 22, 2013 21:25 |
|
Two quick questions: If I wanted to print every letter in the alphabet, how would I do it? I can do this: Python code:
Also, I wanted to select some elements from inside a dictionary into a tuple. I have this: Python code:
|
# ? Nov 23, 2013 06:22 |
|
tractor fanatic posted:Two quick questions: Import the string module and use it, e.g: code:
quote:Also, I wanted to select some elements from inside a dictionary into a tuple. I have this: Why don't you just do this as a,b,c = dict['x'],dict['y'],dict['z']? Do you need a function for this? You aren't really making a tuple anyway, you're unpacking it into three different variables. FoiledAgain fucked around with this message at 06:38 on Nov 23, 2013 |
# ? Nov 23, 2013 06:34 |
|
Dominoes posted:Yes. It's straightforward, and won't overwhelm or confuse someone who's new. When you're learning, new concepts blur together; it's easy to lose sight of what the core concepts are when you introduce too many things at once. It's not a big deal either way, but this is why I'd prefer recommending a clean Python install. I could see someone new not grasping the concept of where Python ends and Anaconda starts, what are quirks for setting up the IDE and what are language concepts etc. I recently tried and failed miserably at a Django tutorial, because it introduced virtual environments, versioning, databases, PaaS, third-party tools etc at once, without making it clear what was actually Django. Not a perfect comparison, but I think it applies. Python.org and Anaconda and Pythonxy each come with one or more IDEs, the Python.org IDE (IDLE) is just a bit too bare bones to be useful, in my opinion. That said, the Windows command line is the worst of all options and I don't know how you can seriously suggest that it's what new users should use. Thermopyle posted:I'm curious about this distributions-vs-python.org thing, so I'm in the middle of spinning up some VMs to test poo poo out. I think that's a good idea.
|
# ? Nov 23, 2013 07:41 |
|
QuarkJets posted:Python.org and Anaconda and Pythonxy each come with one or more IDEs, the Python.org IDE (IDLE) is just a bit too bare bones to be useful, in my opinion. That said, the Windows command line is the worst of all options and I don't know how you can seriously suggest that it's what new users should use. I think that's exactly what new users should use, but I think its quite arguable.
|
# ? Nov 23, 2013 19:01 |
|
I've been storing historical stock data in a 130mb and growing json file, which I import/export from a python dict, which stays in memory while my program's running. Info is stored in the format {symbols: {dates: {attributes: values}}} . Loading the file is slow, and saving it is slower. Would I benefit from a different storage method? Does SQLite sound appropriate for this? The names of the attributes directly affect file size and performance. I've taken to using a shorthand, like replacing 'close price' with 'c'. This seems to indicate that JSON may not be an appropriate format.
|
# ? Nov 23, 2013 19:52 |
|
Dominoes posted:I've been storing historical stock data in a 130mb and growing json file, which I import/export from a python dict, which stays in memory while my program's running. Info is stored in the format {symbols: {dates: {attributes: values}}} . Loading the file is slow, and saving it is slower. This is definitely a situation where an SQLite database would be appropriate. If you ever start to think about deploying your tool onto a website or across a large number of simultaneous users or something then you might need to move up to some sort of enterprise database
|
# ? Nov 23, 2013 20:59 |
|
Thermopyle posted:I think that's exactly what new users should use, but I think its quite arguable. I still don't understand why. Why? Setting up the Windows PYTHONPATH on your own and then messing around in the clunky Windows command line utility is not an enjoyable experience for anyone (it is nothing like a Linux terminal)
|
# ? Nov 23, 2013 21:02 |
|
QuarkJets posted:I still don't understand why. Why? Setting up the Windows PYTHONPATH on your own and then messing around in the clunky Windows command line utility is not an enjoyable experience for anyone (it is nothing like a Linux terminal) Because it keeps you from understanding, and it's not hard unless not only are you new to programming but new to computers. Additionally, all the tutorials and help you find out there are going to know jack squat about your IDE.
|
# ? Nov 23, 2013 21:04 |
|
Dominoes posted:I've been storing historical stock data in a 130mb and growing json file, which I import/export from a python dict, which stays in memory while my program's running. Info is stored in the format {symbols: {dates: {attributes: values}}} . Loading the file is slow, and saving it is slower. I don't mean to discourage you from SQLite because it is completely appropriate for this task but since you already have your data stored as json transitioning to mongodb would likely be very easy (mongodb stores data in json format). Using a relational database would mean that you have to learn something about how to design tables for a relational db. Again, that's not bad, in fact it's a really good thing to learn, but mongodb would be easier.
|
# ? Nov 23, 2013 21:14 |
|
Thermopyle posted:Because it keeps you from understanding, and it's not hard unless not only are you new to programming but new to computers. Additionally, all the tutorials and help you find out there are going to know jack squat about your IDE. Keeps you from understanding what?
|
# ? Nov 23, 2013 21:22 |
|
QuarkJets posted:Keeps you from understanding what? How to janitor your Windows computer, a vital skill for any Python programmer.
|
# ? Nov 23, 2013 21:25 |
|
If someone is going to learn python on windows they probably should know about the command line. Some kind of IDE is not a bad idea too since editing with notepad is garbage. Whether or not someone uses an IDE to run their programs is up to them. Some people like it, some people don't.
|
# ? Nov 23, 2013 21:32 |
|
QuarkJets posted:Keeps you from understanding what? Python. Learning programming concepts, Python syntax and whatnot, and all the concepts inherent in modern Python IDE's is too much to introduce.
|
# ? Nov 23, 2013 22:01 |
|
Dominoes posted:I've been storing historical stock data in a 130mb and growing json file, which I import/export from a python dict, which stays in memory while my program's running. Info is stored in the format {symbols: {dates: {attributes: values}}} . Loading the file is slow, and saving it is slower.
|
# ? Nov 23, 2013 22:02 |
|
^^^ Agreed, HDF5 is a pretty rad format.Thermopyle posted:Python. Having to set up your Windows python environment by hand and then running things from the command line is going to require introducing a lot more non-Python concepts than using a simple IDE like Spyder, which is basically a text editor and a Python terminal with the OS environment properly set up for you. No additional concepts need to be taught. You're getting hung up on the additional features of an IDE, but those are all optional. A new user doesn't need to be introduced to any IDE concepts, and many of the Python IDEs are incredibly intuitive and let you start learning Python immediately. The roadblocks that you're imagining are... imagined Basically what I'm saying is that the Notepad and Windows cmd route takes all of the fun out of Python programming and requires a new user to go through a lot of unnecessary bullshit, whereas a basic IDE lets you get started and learning Python right away. Even the distributors of Python recognize that, which is why the Python.org distribution comes with an IDE
|
# ? Nov 23, 2013 22:33 |
|
I use both the Windows console and PyCharm, but when I started learning I was using just Notepad++ and the console. (I later moved up to Spyder before switching to PyCharm.) One of the early and recurring negative experiences I had with the Windows console is its inability to display any Unicode or Windows characters. I spent far too much time trying to understand what was wrong with some piece of code only to discover that there was nothing wrong with the code at all. The problem was "print"-ing to a crappy console. An IDE (even IDLE) can at least run code:
A full IDE like PyCharm can be overwhelming for sure, but it can be used as just an editor and output console without learning much about the rest of the IDE -- there's still plenty of stuff in it I've never used at all. While learning, I appreciated its auto-inspection to catch stupid typos and its "nagging" about missing docstrings, line length, etc. to remind me about good coding habits. When I ignore the guidelines, it's a conscious choice. It's also much easier to set breakpoints, step through and watch variables in a GUI for code as it starts to get more complex than it is at the console level.
|
# ? Nov 23, 2013 23:14 |
|
|
# ? Jun 13, 2024 05:52 |
|
QuarkJets posted:Basically what I'm saying is that the Notepad and Windows cmd route takes all of the fun out of Python programming and requires a new user to go through a lot of unnecessary bullshit, whereas a basic IDE lets you get started and learning Python right away. Even the distributors of Python recognize that, which is why the Python.org distribution comes with an IDE This. Insisting new users start with the command line and vanilla packages is like insisting that people learn assembly before a higher level programming language.
|
# ? Nov 23, 2013 23:25 |