|
KICK BAMA KICK posted:OK, I'll concede the PEP-8 point. I never actually use a named lambda in reality, that was just for illustration of the concept, but would you really rather see a full-fledged def than a lambda assigned to a name on its own line for something that's simple but barely too long to comfortably fit an an anonymous argument as like the key of sorted? Cause despite whatever PEP-8 has to say, the latter seems way more readable to me. Judgment is key here. I don't think one is fundamentally more unreadable than the other, it heavily depends on the context. For cases involving something like sorted(steam_inventory, key=lambda game: game.price), the lambda is perfectly fine, and I prefer it over "fieldgetter" or whatever the dumb operators function is. If you were doing something like: Python code:
|
# ? Jan 17, 2015 06:59 |
|
|
# ? Jun 13, 2024 06:16 |
|
Suspicious Dish posted:For cases involving something like sorted(steam_inventory, key=lambda game: game.price), the lambda is perfectly fine, and I prefer it over "fieldgetter" or whatever the dumb operators function is. I got used to operator.attrgetter because in cases like this I never want to sort by only one attribute so sorted(steam_inventory, key=attrgetter(price, name)) looks less complicated to me than sorted(steam_inventory, key=lambda game: (game.price, game.name)) (it's also faster if you're silly to be into python code micro-optimization)
|
# ? Jan 17, 2015 08:24 |
|
Suspicious Dish posted:Judgment is key here. I don't think one is fundamentally more unreadable than the other, it heavily depends on the context.
|
# ? Jan 17, 2015 08:30 |
|
KICK BAMA KICK posted:OK, I'll concede the PEP-8 point. I never actually use a named lambda in reality, that was just for illustration of the concept, but would you really rather see a full-fledged def than a lambda assigned to a name on its own line for something that's simple but barely too long to comfortably fit an an anonymous argument as like the key of sorted? Cause despite whatever PEP-8 has to say, the latter seems way more readable to me. Also note you are allowed to do one-line def statements Python code:
Python code:
|
# ? Jan 17, 2015 17:19 |
|
Suspicious Dish posted:Don't write Lisp in Python, please. I've never written Lisp and it looks indecipherable to me. Whereas what I would wrote appears pretty obvious even to someone with little functional experience (I think). That said, the moment someone pays me to write Python for them, I will stop doing that kind of thing. But as long as I'm just writing data analysis scripts for myself, I find it a real easy approach. Anyways, a real question. Compare Python code:
Python code:
|
# ? Jan 17, 2015 17:30 |
|
SurgicalOntologist posted:Oh but wait, is it ever closed? SurgicalOntologist posted:Because in the second version, the filename object is never assigned a name so it's immediately garbage collected. \/ it does, I got confused. But I'm still not a big fan of open() one-liners. Symbolic Butt fucked around with this message at 18:06 on Jan 17, 2015 |
# ? Jan 17, 2015 17:53 |
|
Symbolic Butt posted:The file is closed when the object is garbage collected. However...
|
# ? Jan 17, 2015 17:59 |
|
Can you guys clarify? Are you saying that (1) it gets closed when it gets garbage collected (that would make sense) but (2) it does not get garbage collected until the function returns? That makes less sense. What you think about the advisability of it? I see it for example in setup.py with something like long_description=open('README.rst').read(). I guess the question is if all you're doing with the file object before it gets out of scope is calling the read method, can that method fail? If so you'd want to use with to close it in case it does fail. E: saw your edit. I'm inclined to agree. I guess this same argument could be made against my functional version as well, come to think of it. I guess you could make a safe_read function that encapsulates the with statement with the read method.
|
# ? Jan 17, 2015 18:05 |
|
SurgicalOntologist posted:I've never written Lisp and it looks indecipherable to me. Whereas what I would wrote appears pretty obvious even to someone with little functional experience (I think). These are pretty much equivalent in cpython, but garbage collection doesn't necessarily work the same across all implementations. In pypy, for instance, the file wouldn't immediately be closed in the second function.
|
# ? Jan 18, 2015 20:19 |
|
A few things from the current project I'm playing around with... 1. Authentication over JSON-RPC I'm trying to build a system where the application logic and the UI are totally separated, with all information/updates between them through JSON-RPC calls. I'd like to keep the user account/authentication in the application logic layer, both to avoid each UI having to implement its own user database, and because I'd like the flexibility of being able to do different things based on the user that is calling the service. The first method of doing this that comes to mind would be to create user/session tables in the application DB along with a service that would pass back a session token for a given user upon correct credentials being supplied, which would then need to be used in all subsequent service calls to identify that user. However, this seems like a problem that would have been solved in the past and better then I could manage, so am I missing something obvious? 2. SQLAlchemy Sessions In the application layer code I have so far, I have a file which contains the SQL database code like so: Python code:
Python code:
3. Auto Incrementing Numbers & SQLAlchemy In the address_data table referenced above, the PK for it is set up as an auto incrementing integer in the Postgres DB. When doing some testing, I discovered that if I make a .add( ) call to the session, but not a .commit() after, it will still increment that integer and effectively I've lost that reference since there will never be a record for it. Is there a way to avoid this, or is there a good reason for it to work in that fashion?
|
# ? Jan 19, 2015 20:14 |
|
Begall posted:2. SQLAlchemy Sessions
|
# ? Jan 19, 2015 20:25 |
|
KICK BAMA KICK posted:I'm not quite sure what you're doing here in the larger picture -- is your code interacting with a pre-existing database whose schema you don't have any control over (a guess from the Base.metadata.reflect)? I'm confused as to what the address_data (got a good reason for an unconventional class name like that?) and Address classes are supposed to represent. I do have control over the schema, but I've been making changes to it quite rapidly through a web admin tool, so it seemed easier to just reflect it rather than go through and update the code each time. address_data is simply the name of the table, and represents the table so that I can do things like: Python code:
|
# ? Jan 19, 2015 20:35 |
|
I'm still not sure if I'm completely misunderstanding what you're doing -- entirely possible, I've just been teaching myself SQLAlchemy over the course of my own pet project with no prior coding experience -- or if maybe you're misapplying some SQLAlchemy concepts and have gotten yourself kinda stuck halfway between the declarative way of doing things and the other way? Like is there more to the definition or use of address_data or could you just be using a Table object? Does an instance of Address represent a single address (whatever that means in your domain) corresponding to a single record in the address_data table? If so, it seems like the declarative way of doing this would be to scrap that address_data intermediary and make Address to itself be the mapped class inheriting from Base, which you could use in queries the same way you're using address_data. If not, at least the way I've been working, address_data is the thing that should be called Address and I wouldn't know what to call what you're calling Address without understanding what exactly it is. That's before we get to the question about the Session itself, which was pretty thorny for me to work out in my own experience too.
|
# ? Jan 19, 2015 21:24 |
|
Begall posted:I do have control over the schema, but I've been making changes to it quite rapidly through a web admin tool, so it seemed easier to just reflect it rather than go through and update the code each time. address_data is simply the name of the table, and represents the table so that I can do things like: You should look at alembic. It generates migrations for your database (creating tables/fields, altering them, etc.) based on your SQLAlchemy models.
|
# ? Jan 20, 2015 16:55 |
|
What's the best way to dynamically add more subclasses (and by extension, remove them) to an existing class? Right now I have a generic 'Entity' class that inherits a number of mixin classes to provide it with specific utility - this mixin handles if and how something moves, this mixin has variables and methods that determine that it's a biological construct, etc. However, I'd like the ability to dynamically add and remove other mixins during runtime. Adding the new classes as a variable is possible but the A.B.C.whatever() syntax gets messy and it's hard to programmatically determine whether or not a particular class has the mixin class added. A dict in the base Entity class that the mixins get added to? That lets me add and remove things and also the ability to determine if the mixin class exists attached to the base class and provide a default if it doesn't, so that's what I'm leaning towards, but I dunno if there's an easier / more obvious route that I'm not seeing.
|
# ? Jan 21, 2015 13:23 |
|
I'm using the builtin logging module to ... well ... log stuff and do some informational messages for the user. I was writing a few simple convenience functions for myself and in the process was trying to extend the logger behaviour (add in some more format specifiers, etc.) Except: while you can define a Logger class for the logging to use, this doesn't get used as the root logger, because by simply importing logging, you instantiate the root logger. Which is odd behaviour and seems to imply that you can never redefine or override the root logger. Googling shows other people coming to the realisation, with one or two cryptic reports that others have "got around it". Is this right?
|
# ? Jan 21, 2015 17:47 |
|
outlier posted:I'm using the builtin logging module to ... well ... log stuff and do some informational messages for the user. I was writing a few simple convenience functions for myself and in the process was trying to extend the logger behaviour (add in some more format specifiers, etc.) Instead of using the root logger, you call methods of your class instead of functions from the logging module (which are aliased to method calls of the root Logger instance). You could also redefine the root Logger instance, but that's unnecessary. Python code:
|
# ? Jan 22, 2015 07:48 |
|
Maybe it's just really early, but I don't understand why this happens:code:
|
# ? Jan 22, 2015 10:38 |
|
FoiledAgain posted:Maybe it's just really early, but I don't understand why this happens: Python code:
If it's a simple value (int, bool, float, string) it will copy "by value", so it a = b (when both bool) won't mean that a refers to the same object as b, so changing b will not change a. If it's an object of some kind, then it actually copies "by reference", so they both refer to the same object, in which a = b means that changing b will actually change a. Imagine "by reference" as the variable holding a kind of object ID that python uses internally to route to a specific instance, so a = b just makes them both have the same "id". Jewel fucked around with this message at 11:03 on Jan 22, 2015 |
# ? Jan 22, 2015 10:59 |
|
This is the kind of thing where pythontutor can really help you understand it in a more visual way: http://www.pythontutor.com/visualiz...B%5D&curInstr=0 Then try doing stuff like code:
|
# ? Jan 22, 2015 11:03 |
|
Symbolic Butt posted:This is the kind of thing where pythontutor can really help you understand it in a more visual way: Oh that's a realllly nice site, thanks for that. It helps to look at mine too, see how they "point" to the new object http://www.pythontutor.com/visualiz...B%5D&curInstr=3
|
# ? Jan 22, 2015 11:05 |
|
FoiledAgain posted:Maybe it's just really early, but I don't understand why this happens: The list does not hold a variable, it holds values. You put False, False into flags.
|
# ? Jan 22, 2015 11:28 |
|
QuarkJets posted:Instead of using the root logger, you call methods of your class instead of functions from the logging module (which are aliased to method calls of the root Logger instance). You could also redefine the root Logger instance, but that's unnecessary. That's true - i may be overthinking this thing. 90% of the time, I'll just be using a single logger so why not do it this way. But it's very strange that you can't change the root logger, so if you use subloggers, they will be behaving differently to the root. (The original problem I was trying to solve is that formats are specific to handlers not levels, so a DEBUG message will look the same as an informational one. Irritating more than essential.)
|
# ? Jan 22, 2015 11:48 |
|
I've been doing more development. I've become very tired of figuring out layouts for tkinter programs. Which GUI designer is the best?
|
# ? Jan 22, 2015 15:51 |
|
SYSV Fanfic posted:I've been doing more development. I've become very tired of figuring out layouts for tkinter programs. Which GUI designer is the best? Qt Designer.
|
# ? Jan 22, 2015 16:11 |
|
Dominoes posted:Qt Designer. Prefer to keep working with tkinter at the moment.
|
# ? Jan 22, 2015 16:42 |
|
You're stuck. tkinter is bad.
|
# ? Jan 22, 2015 17:59 |
|
Suspicious Dish posted:You're stuck. tkinter is bad. Yeah, next application or refactor I guess I will learn QT.
|
# ? Jan 22, 2015 18:17 |
|
SYSV Fanfic posted:Yeah, next application or refactor I guess I will learn QT. It's similar to Tkinter in a lot of ways
|
# ? Jan 22, 2015 18:50 |
|
Which leads to: I had an idea for a program other people might contribute to and even possibly use. Are there any non performance drawbacks to writing something that could cause headaches as the project grows? Being unable to prevent access to class internals doesn't seem like a problem if all code is reviewed for it before a commit.
|
# ? Jan 23, 2015 16:18 |
|
SYSV Fanfic posted:Which leads to: I had an idea for a program other people might contribute to and even possibly use. Are there any non performance drawbacks to writing something that could cause headaches as the project grows? I don't fully understand what you are asking in the bolded statement.
|
# ? Jan 23, 2015 19:13 |
|
accipter posted:I don't fully understand what you are asking in the bolded statement. Problems apart from performance. How well does python scale as the complexity ramps up?
|
# ? Jan 23, 2015 19:16 |
|
SYSV Fanfic posted:Problems apart from performance. How well does python scale as the complexity ramps up? Pretty well, there are some big Python projects on github. Look at this sweet project with 92 contributors: "Luigi is a Python module that helps you build complex pipelines of batch jobs. It handles dependency resolution, workflow management, visualization etc. It also comes with Hadoop support built in." They mention further down that Luigi is used at Spotify, and this github was apparently started by Spotify employees. https://github.com/spotify/luigi If you're working on a project that becomes complex, you might consider using a nice IDE like PyCharm to help you manage things.
|
# ? Jan 23, 2015 19:24 |
|
Since QT came up recently, can anyone give a quick "for dummies" summary of the QT 4/5 versions and licenses? When I looked into it a long while back, I got confused about all the Nokia/commercial/GPL stuff. I've avoided doing anything with QT since out of uncertainty about how I could actually use and/or release something using it. Tkinter, while generally terrible and ugly, I at least know is safe to use.
|
# ? Jan 24, 2015 00:17 |
|
onionradish posted:Since QT came up recently, can anyone give a quick "for dummies" summary of the QT 4/5 versions and licenses? When I looked into it a long while back, I got confused about all the Nokia/commercial/GPL stuff. I've avoided doing anything with QT since out of uncertainty about how I could actually use and/or release something using it. Tkinter, while generally terrible and ugly, I at least know is safe to use.
|
# ? Jan 24, 2015 10:46 |
|
Dominoes posted:PyQt is Qt 4 or Qt 5, and the free version is licensed under GPL (ie software that uses yours must be open sorce), or a commercial license you pay for. According to a cursory google search (I am not a lawyer), it's actually unclear whether a work that uses GPL-licensed software must be open source. The GPL refers to derivative work, which is poorly defined. Is a derivative work one in which you modify the GPL-licensed source code as part of your own project's development? Is a derivative work one in which you merely link to a GPL-licensed library? The Free Software Foundation claims that both of these are derivative works, but I and many others would disagree with the second one. Legally, I think it's an open question. However, everyone agrees that under the terms of the GPL your work doesn't have to be publicly available, it just needs to be open source. I think that this is an important distinction to make. By contrast, the LGPL is basically the GPL but explicitly allows propriety software to use or modify your open source work. So when in doubt, you should probably use LGPL (or BSD or any of a hundred other "non-copyleft" software licenses) libraries. Here's an article that brings up the derivative work question http://www.linuxjournal.com/article/6366 And here's a relevant question from GNU's Q&A quote:You have a GPL'ed program that I'd like to link with my code to build a proprietary program. Does the fact that I link with your program mean I have to GPL my program? (#LinkingWithGPL) This is how PySide, which uses the GPL-licensed Qt, can be released under the less-restrictive LGPL. The LGPL explicitly allows proprietary software to be created from open source software; the open source software must remain open source, but the software linking to the open source libraries can be closed source. Basically, if you want to be create something without ever releasing the source code for it, don't use GPL-licensed software. However, the GPL is permissive about selling software, so if you don't mind handing over the source code to whoever buys a copy then don't worry about the license. Also, nothing in the GPL requires that your software be publicly available. That's another important fact that I feel should be mentioned. e: tl;dr software licensing is a confusing mess of legal terms, if you want to avoid potential legal hurdles then don't use GPL-licensed libraries in your closed source project. PySide is LGPL, so it would be safe to use QuarkJets fucked around with this message at 21:39 on Jan 24, 2015 |
# ? Jan 24, 2015 20:40 |
|
I'm using Anaconda for my Python distribution. To use the Intel MKL on Python, I either have to by Anaconda's optimizer package thing, or compile Numpy manually, correct? And the latter option is probably highly dispreferred as I'll have a good chance messing up Anaconda's infrastructure?
|
# ? Jan 27, 2015 11:06 |
|
Cingulate posted:I'm using Anaconda for my Python distribution. To use the Intel MKL on Python, I either have to by Anaconda's optimizer package thing, or compile Numpy manually, correct? And the latter option is probably highly dispreferred as I'll have a good chance messing up Anaconda's infrastructure? code:
However, MKL is a commercial library originally sold by Intel, it's not part of "stock" NumPy. We are licensed to re-distribute MKL (and we do, through the add-on package we sell). If you want to use MKL "by hand" you definitely can, but you'd have to not just compile NumPy yourself, but also purchase MKL directly from Intel, and then configure NumPy to use it (the default build settings use an OSS linear algebra library). Edit: though maybe you are asking because you already own an MKL license and want to use it. If so, it would make perfect sense to want to compile your own. Let me know if you have any specific questions, I haven't built NumPy myself in a while but I'm sure I could ping someone who does. BigRedDot fucked around with this message at 18:47 on Jan 27, 2015 |
# ? Jan 27, 2015 18:43 |
|
Yes, I already have the MKL and actually just compiled R to make use of it (... instead of going the comfortable route and downloading Revolution Analytics' R distribution). I've also set up a few conda envs (thanks to this thread) - though may I ask in this context how I can remove an entire environment at once over the CLI? I'm just wondering if a mostly computer-illiterate person such as I should even bother trying to get MKL and an Anaconda Python to play along nicely by hand (e.g. by manually compiling Numpy), or if I should just go for my credit card. I understand correctly you're working for continuum.io? Cingulate fucked around with this message at 19:51 on Jan 27, 2015 |
# ? Jan 27, 2015 19:47 |
|
|
# ? Jun 13, 2024 06:16 |
|
Cingulate posted:I understand correctly you're working for continuum.io? I was noticing that too, it's pretty nice to have a guy from there in this thread if true. I use anaconda regularly for work. Spyder owns and it's nice to have all the modules I need in one stack.
|
# ? Jan 28, 2015 00:32 |