|
Closures are a poor man's objects. Objects are a poor man's closures.
|
# ? Oct 26, 2013 21:03 |
|
|
# ? May 30, 2024 21:02 |
|
Dominoes posted:For example, in an algorithmic stock trader I'm working on, I have a variable called transactions, which is a list of objects that describe previous stock transactions. This variable is referenced many times, in multiple functions. While I could keep it alive in the main run loop and pass it around as an argument, doing so would be somewhat confusing and awkward. It would lead to cases where functions with a logical parameter list take transactions as additional argument. Ie a function that places orders seems to just need two arguments: The stock symbol and quantity. Instead, it, like many other functions, requires transactions as well, since it calls a save function that requires the object list to be saved to disk after placing the order. It feels like a violation of DRY, and makes changing things more confusing. Do you ever place an order and not want to log it in transactions? If the answer is no you probably want an object called TransactionManager or something that is one time initialized with historical data and from then on all order operations are run through its methods. e.g. Python code:
|
# ? Oct 26, 2013 21:25 |
|
Dominoes posted:For example, in an algorithmic stock trader I'm working on, I have a variable called transactions, which is a list of objects that describe previous stock transactions. This variable is referenced many times, in multiple functions. While I could keep it alive in the main run loop and pass it around as an argument, doing so would be somewhat confusing and awkward. It would lead to cases where functions with a logical parameter list take transactions as additional argument. Ie a function that places orders seems to just need two arguments: The stock symbol and quantity. Instead, it, like many other functions, requires transactions as well, since it calls a save function that requires the object list to be saved to disk after placing the order. It feels like a violation of DRY, and makes changing things more confusing. Well, what happens if you want to have multiple transaction queues, e.g. if you're trading on both the NYSE and the NASDAQ? You need to be able to say "please put your transactions on this queue" somehow. I don't see what's wrong with passing it as an argument. Dren's solution using objects is also acceptable. In here, you marry together the idea of a transaction queue with the methods for manipulating and appending to it, and it comes with a bit of nice syntax. But you said you aren't comfortable with OOP, so you probably don't like that one. (also I hope you aren't writing a stock trader for actual reals. You won't get anywhere with Python.)
|
# ? Oct 26, 2013 22:08 |
|
Dren posted:Do you ever place an order and not want to log it in transactions? If the answer is no you probably want an object called TransactionManager or something that is one time initialized with historical data and from then on all order operations are run through its methods. e.g. Suspicious Dish posted:Well, what happens if you want to have multiple transaction queues, e.g. if you're trading on both the NYSE and the NASDAQ? You need to be able to say "please put your transactions on this queue" somehow. I don't see what's wrong with passing it as an argument. quote:(also I hope you aren't writing a stock trader for actual reals. You won't get anywhere with Python.) Dominoes fucked around with this message at 22:54 on Oct 26, 2013 |
# ? Oct 26, 2013 22:32 |
|
If you go with an object like I suggested keep in mind that a key part of the design is that the transaction state associated with the manager can only ever be set/loaded via the manager's constructor. (There is no "load" method, only a "save" method). This keeps the object's users (probably just you but w/e) from trying to reuse an instance, not properly resetting all of the state associated w/ it, and getting into trouble. I have seen too many avoidable bugs caused by reused objects not properly reset and every time it disappoints me because unless there is some resource reason to reuse objects a new one could be made instead, which is simpler and far less error prone.
|
# ? Oct 27, 2013 04:02 |
|
Dumb PyQt question: I have two spinboxes set up to change the size of an image, and when the user edits one of the spinboxes, the other one is automatically adjusted based on the image's aspect ratio. Both spinboxes are connected to the method that handles that using the valueChanged signal, and the method uses setText to change the value in the other spinbox. The problem being that setText emits a valueChanged signal, which calls the method again, and it keeps repeating until one of the spinboxes is at its minimum or maximum value. I'm sure there's a simple way to get around this, but I can't find anything.
|
# ? Oct 27, 2013 07:31 |
|
Masa posted:Dumb PyQt question: If you're checking to make sure that the aspect ratio is the same, then there shouldn't be any issue. For instance, if the aspect ratio is 1 and I decrease a 100x100 image to 99x100, then the aspect ratio is no longer correct (0.99); I change the other value accordingly and the image becomes 99x99. This will cause the valueChanged signal to get emitted again, but now the aspect ratio is 1 again, so nothing should happen. Make sure that your aspect ratio checking logic is working correctly. On non-unity aspect ratios you're going to have to do more complicated things (what happens when your aspect ratio is 0.3 and you change a 3x10 image to 3x9? You'll never get back to 0.3 aspect ratio just by decreasing the other image dimension)
|
# ? Oct 27, 2013 07:51 |
|
Masa posted:Dumb PyQt question: Here's a standalone program that does what you ask. The key bits are the blockSignals calls in the set_width and set_height methods. Python code:
Dominoes fucked around with this message at 14:38 on Oct 27, 2013 |
# ? Oct 27, 2013 09:22 |
|
SurgicalOntologist posted:I just finished the first working prototype of a small project; is it kosher to post [a link to] my code here looking for critiques? Or should that go in project.log? Whatever, I'm just posting it here. If this should go somewhere else, please let me know: https://bitbucket.org/hharrison/experimentator This is my first Python project that is meant to be used by someone else, but it's still mostly a learning exercise so I would really appreciate any critique/feedback. I could also use some guidance writing tests for this. I don't even know where to start so maybe some reading on the topic would be most helpful.
|
# ? Oct 27, 2013 19:03 |
|
M31 posted:It's not a strange rounding issue, it's a limitation of using floating point numbers. More specifically, 0.1 can't be represented as a floating point number, so you get something that is very close, but not actually 0.1. Thanks for the explanation. I've really never run into a problem with using floating point steps in arange, so that's why I called it odd.
|
# ? Oct 28, 2013 17:38 |
|
SurgicalOntologist posted:This is my first Python project that is meant to be used by someone else, but it's still mostly a learning exercise so I would really appreciate any critique/feedback. I could also use some guidance writing tests for this. I don't even know where to start so maybe some reading on the topic would be most helpful. A couple of minor remarks: Line 74, change 'value' to '__getitem__'. It's confusing when code:
code:
Look at the abc module (http://docs.python.org/2/library/abc.html). Use it for the Experiment class and make people override run_trial Line 148, I would express this as code:
|
# ? Oct 28, 2013 21:54 |
|
thorf posted:A couple of minor remarks: Thanks, much appreciated! I think I understanding the abstract class thing, is it basically: - let the user know that they shouldn't make instances of a class but instead subclass it (and enforce this?) - enforce which methods must be overridden. If that's correct, it seems to make sense to make Variable an abstract class too. My example still works, so things are good. I guess my next move is to make a more complex example that uses more features, so I can use that as a test also - at least until I figure out how unit testing is supposed to happen.
|
# ? Oct 28, 2013 23:38 |
|
Suspicious Dish posted:(also I hope you aren't writing a stock trader for actual reals. You won't get anywhere with Python.) This is very wrong. A number of very successful firms have used trading platforms written in Python.
|
# ? Oct 29, 2013 01:58 |
|
Has anyone tried using pyfacebook or similar? Say I wanted to make a markov chain generator which posts to Facebook statuses/comments, is that doable?
|
# ? Oct 30, 2013 17:04 |
Is there an easy way to crop and resize animated gifs in Python? I found images2gif but I'm wondering if there's something that's...a little better supported. Maybe I'm better off going with some external utility like ImageMagick?
|
|
# ? Oct 30, 2013 23:50 |
|
low quality jpeg posted:Has anyone tried using pyfacebook or similar? I've never used it but the last time I tried Facebook integration it was basic OAuth token generation and was pretty easy to work with.
|
# ? Oct 31, 2013 04:27 |
|
If I'm using downloaded Python on a Mac and not the Apple-supplied one, is installing modules as big of a pain in the rear end as it seems? I only really use it to automate work tasks and make dumb games (which modules like pygame or easygui would help with). I've googled around a little, and it seems to involve downloading a bunch of things with a bunch of jargon I don't understand. I have zero background in computer science.
|
# ? Oct 31, 2013 04:49 |
|
User-Friendly posted:If I'm using downloaded Python on a Mac and not the Apple-supplied one, is installing modules as big of a pain in the rear end as it seems? I only really use it to automate work tasks and make dumb games (which modules like pygame or easygui would help with). No, it's actually not that bad. Follow this guide. http://docs.python-guide.org/en/latest/starting/install/osx/
|
# ? Oct 31, 2013 15:31 |
|
I decided to finally start using Python 3 where I can. The very first time I decide to use it on a project, I find out my favorite green threading library, eventlet, doesn't support it. Any good alternatives for that?
|
# ? Oct 31, 2013 20:05 |
|
Thermopyle posted:I decided to finally start using Python 3 where I can. The very first time I decide to use it on a project, I find out my favorite green threading library, eventlet, doesn't support it. Circuits? I know gevent and concurence are also not ready for Python 3. Every year I say I'll try to switch to Python 3, but then I hit yet another library that still doesn't have a good alternative.
|
# ? Oct 31, 2013 20:10 |
|
Wall of text incoming: So I decided my 'turn code for a single trial into code for an experiment by looping over combinations of independent variables' project would be a good opportunity to play with recursive generators and ChainMaps. So I'm in the process of rewriting it, and it's good fun. The idea is that an experiment is defined by sections: Participants contain sessions, sessions contain blocks, and blocks contain trials (by default). An independent variable can live at any level of the hierarchy. It can change every trial (as in a fully randomized within-subjects experiment), every session (in a before-and-after experiment), every participant (in a between-subjects experiment), etc. Each section figures out which sections it contains and runs them. Python code:
There's one more thing I can't figure out: Is it possible to break the recursive loop, save the state to disk, and re-enter it later? If session 0 is ending, the whole experiment needs to suspend and I need to later start again at session 1. If I do Python code:
E: To avoid having to mentally parse that middle part, next_sections is a list of dictionaries, each describing a section one level down (e.g., in a block, each dict contains the variables that need to be set for a trial). SurgicalOntologist fucked around with this message at 22:50 on Oct 31, 2013 |
# ? Oct 31, 2013 22:33 |
|
SurgicalOntologist, it seems like there's a decent amount of code you left out. There also aren't any examples of what the output is supposed to look like. Perhaps I'm thick but having all the code and having examples would help me. Alternatively, a narrower example (even if contrived) could work. To answer your question the best I can, I know of no way to save the state of the interpreter in order to capture your place in the running of your recursion. I have two general suggestions for how to stop and resume your routine. The first is to alter your processing code so that it can accept a partially filled out root_context, skip the parts that have already been filled out, and start back where you left off. This approach has the disadvantage of needing to run through all of the things you've already processed upon resumption in order to skip them. The second is to ditch the actual recursion and do the recursion bookkeeping yourself by using a queue as the recursion stack. Then you'll have access to your recursion stack (the queue) when you want to stop execution and serialize your state. With access to all the state you need you can pick back up right where you left off. Your code led me to learn about yield from and chainmap. I think I hate yield from but that's just because I hate recursion. And chainmap, I guess that's ok. Not sure I've ever needed it.
|
# ? Nov 1, 2013 17:49 |
|
So...config files and versioning. Anyone have any favorite methods they use to track and migrate config files as their application evolves? I'm thinking about stuff like User is using your application at version 1.0. You add new keys/values to your config files and restructure how it's laid out for version 2.0. User upgrades to version 2.0. How do you migrate? Custom code to handle all the possible upgrade paths is hell, but I don't know of any other way. Something like Django's South for config files would be neat.
|
# ? Nov 1, 2013 23:21 |
|
For our backend services we've recently started using zookeeper for all of our configs. When an application starts up it checks into zookeeper and checks out a properties file for it's specific version and environment. We never have to worry about what the upgrade path is like because all of the configs are kept in one place. It also makes managing configs super easy. We've got some custom tools that let us batch update and clone configs to new versions. It was a lot of work to setup, but worth it if your environment can support that.
|
# ? Nov 1, 2013 23:30 |
|
Thermopyle posted:So...config files and versioning. So, basically you have a situation where your application's configuration format/schema is prone to change, and you'd like to provide automation for updating (and possibly downgrading) your third party users' config files to the corresponding version?
|
# ? Nov 1, 2013 23:56 |
|
Dren posted:SurgicalOntologist, it seems like there's a decent amount of code you left out. There also aren't any examples of what the output is supposed to look like. Perhaps I'm thick but having all the code and having examples would help me. Alternatively, a narrower example (even if contrived) could work. Thanks for your help. Here's what I left out: Python code:
E: anyone know why the Python code formatting didn't work on my post above but works here? It looks so ugly without it...
|
# ? Nov 2, 2013 00:30 |
|
HatfulOfHollow posted:For our backend services we've recently started using zookeeper for all of our configs. When an application starts up it checks into zookeeper and checks out a properties file for it's specific version and environment. We never have to worry about what the upgrade path is like because all of the configs are kept in one place. It also makes managing configs super easy. We've got some custom tools that let us batch update and clone configs to new versions. It was a lot of work to setup, but worth it if your environment can support that. zookeeper seems pretty cool, but I'm afraid it's way overkill for me. Haystack posted:So, basically you have a situation where your application's configuration format/schema is prone to change, and you'd like to provide automation for updating (and possibly downgrading) your third party users' config files to the corresponding version? Yeah, that's basically it. So, say the application has a config.ini that looks like: code:
code:
Mainly just looking for strategies or tools or config file formats or any ideas really.
|
# ? Nov 2, 2013 04:05 |
|
I doubt there's any sort of generalized schema migration system that supports arbitrary sorts of config files, because it'd have to be so general that it doesn't actually do much of anything. Assuming you can't just use a config format which already has schema migration tools, I don't think there's really any better answer than creating one.
|
# ? Nov 2, 2013 04:16 |
|
You could store config settings in an XML file and use XSLT to convert between versions. I don't know if I would recommend this though.
|
# ? Nov 2, 2013 07:56 |
|
1. Put Config settings in flat file w/ version field (ini file, json, not XML) 2. Write module that converts from old settings to next version up, only ever allow latest version of config to run (users must convert their configs when they upgrade) 3. Profit
|
# ? Nov 2, 2013 12:32 |
|
Anyone know a good way to teach yourself how to do GUI? I tried googling around a bit but theres a lack of really explaining how to set it up.
|
# ? Nov 2, 2013 17:33 |
|
GUI is not a thing Python is good at. There are multiple solutions and they're all quite bad.
|
# ? Nov 2, 2013 18:00 |
|
Buller posted:Anyone know a good way to teach yourself how to do GUI? I tried googling around a bit but theres a lack of really explaining how to set it up. So, no. If you want to dive into Qt and have questions, ask here.
|
# ? Nov 2, 2013 18:04 |
|
Does anyone have experience getting wxpython to run on OS X? I'm trying to use RIDE (Robot Framework IDE) on OS X, and it depends on wxpython. However, I keep getting segfaults More info at https://groups.google.com/forum/#!topic/robotframework-users/jNUj1ytQXK0
|
# ? Nov 2, 2013 18:12 |
|
Buller posted:Anyone know a good way to teach yourself how to do GUI? I tried googling around a bit but theres a lack of really explaining how to set it up. PyQt and PySide have a wealth of resources on the internet and in published materials that are aimed at GUI design, and they're both based on Qt, which is widely recognized as a good GUI-building framework, so I don't know what the gently caress Suspicious Dish is talking about
|
# ? Nov 2, 2013 20:39 |
|
Buller posted:Anyone know a good way to teach yourself how to do GUI? I tried googling around a bit but theres a lack of really explaining how to set it up. In my experience, tkinter is the easiest Python GUI framework for just hopping in and getting your hands dirty. Its API and documentation are pretty awful, and it's generally a pain to use, but it's built-in and doing things is generally fairly simple. Keep in mind, the first few GUIs you write will be really horrible. The next few might be horrible too.
|
# ? Nov 2, 2013 21:06 |
|
All GUIs are horrible. Some are just less horrible than others.
|
# ? Nov 2, 2013 22:04 |
|
Pollyanna posted:All GUIs are horrible. Some are just less horrible than others.
|
# ? Nov 2, 2013 22:34 |
|
Dominoes posted:Go on. I just hate everything
|
# ? Nov 2, 2013 22:35 |
|
|
# ? May 30, 2024 21:02 |
|
Dominoes posted:Go on. I suggest this in this thread every 40 pages or so: use python with a thin server like cherrypy or flask, make a user's web-browser the way to interact. Templating libraries like Jinja write out html and use javascript where appropriate, hitting methods on the server to do the filesystem things. This is how both http://sabnzbd.org/ and http://sickbeard.com/ do it and they're pretty sweet and usable apps. It's also a lot easier to write automated tests since testing webpages is a bit more developed problem space than native apps in the free/open source only world
|
# ? Nov 3, 2013 03:53 |