|
Suppose I'm working on some application code that's Python 2.7, but I believe sometime in the future it will be ported to Python 3 (but isn't currently as it's waiting on a dependent library to be ported). Now suppose I've written two versions of some code which will look much cleaner in Python 3. What's the best option? Store it in comments? Import six, place it as an alternative if on Python 3, and make it a dead code path? Keep it in a separate file? Keep it out of version control entirely and hope I remember when "eventually" comes?
|
# ? Nov 19, 2014 05:09 |
|
|
# ? May 20, 2024 04:40 |
|
Symbolic Butt posted: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. Can you elaborate on this? What fundamental tradeoff? In what way are attributes the kings of Python and methods are a bumpy ride? Because tef's post (which you quoted and sounded like you were trying to agree with) basically says the opposite of this. Methods are just functions, and that's a good thing.
|
# ? Nov 19, 2014 05:34 |
|
SurgicalOntologist posted:Can you elaborate on this? What fundamental tradeoff? In what way are attributes the kings of Python and methods are a bumpy ride? Because tef's post (which you quoted and sounded like you were trying to agree with) basically says the opposite of this. Methods are just functions, and that's a good thing. I'm terrible at expressing myself but let me try anyway: With python objects the attributes comes first. Those attributes can be functions with lexically scoped self. In contrast with ruby objects the methods comes first. There's no real functions and everything is a method (even assignment). That's the tradeoff that I was referring about, python gave preference to attributes as the most fundamental element of the object system. Implicit self for example is impossible with this choice (see that one post by Guido). I think when you center your code around methods, writing ruby does feel nicer. But then you lose some introspection power that you have in python. I mean, when everything is a method you can't just get near them without calling them in a nice way. You have to wrap with a block/proc something. In python you can just pass some_object.some_method around without the parenthesis and that's it.
|
# ? Nov 19, 2014 06:25 |
|
ShadowHawk posted:Suppose I'm working on some application code that's Python 2.7, but I believe sometime in the future it will be ported to Python 3 (but isn't currently as it's waiting on a dependent library to be ported). In these cases I just write python2 code with all the relevant 'from __future__ import's. It's not reaaally python3 but maintaining 2 separate versions is just too much hassle.
|
# ? Nov 19, 2014 06:31 |
|
Symbolic Butt posted: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. I don't understand this. What's bumpy about writing methods? edit: oops, there's another page. I still don't get where you're coming from.
|
# ? Nov 19, 2014 19:01 |
|
Thermopyle posted:I don't understand this. What's bumpy about writing methods? It's more annoying to write self.property than simply property as you would in most languages with the concept of a class method, for one.
|
# ? Nov 19, 2014 23:19 |
|
Munkeymon posted:It's more annoying to write self.property than simply property as you would in most languages with the concept of a class method, for one. Well yeah. I guess I just feel there's a vast chasm between having to write self and a "bumpy ride". I can see how that would be subjective, though.
|
# ? Nov 20, 2014 00:48 |
|
Allow me to interrupt this self talk to to ask a selfish work related problem. I have a number of different modules with a bunch of unittest TestCases for an couple embedded products within them. I have a test runner script that runs various combinations of tests and constructs different test reports. Ideally, I'd like the test runner to initialize the hardware interfaces and pass them to the modules with test cases and have those test cases use those hardware interfaces to perform their tests. Is this feasible? I've tried a bunch of hacky things and can't even get globals to propagate in to the test cases. I'm really just trying to get away from having a bunch of duplicated setup/teardown code in each of the test case modules as well as making things configurable without having a bunch of duplicated argparser code. This is driving me nuts, so if you know what I'm talking about but explaining it feels like too much effort, please, even a terse response that sets me in the right direction would be appreciated, thanks.
|
# ? Nov 20, 2014 00:49 |
|
Why did python choose that design philosophy in the first place, actually? Most other languages, at least that I use, implicitly give "this" to a function in a class, and if python forces you to have a keyword representing "this"/"self" in every member function, why not just provide it via a builtin keyword instead of making you add the argument manually every time? And then to go a step further, why not implicitly scope local arguments on the class. In C# you can type "Property = true" but you can also type "this.Property = true" if you want to avoid clashes with argument names, where "Property" will, by default, scope to be the passed in argument rather than the member of the same name. This wouldn't even remove the ability to set "self.someProperty" in the init function and have it set a member variable (which would keep the ability to just reference it by "someProperty" in any other functions, unless you want to keep the pythonic "always use self to reference a member variable" which, personally I think is unnecessary as the only thing it lets you do is have a local and a member variable with the same name which seems dirty).
|
# ? Nov 20, 2014 00:58 |
|
sund posted:Allow me to interrupt this self talk to to ask a selfish work related problem. I have a number of different modules with a bunch of unittest TestCases for an couple embedded products within them. I have a test runner script that runs various combinations of tests and constructs different test reports. Ideally, I'd like the test runner to initialize the hardware interfaces and pass them to the modules with test cases and have those test cases use those hardware interfaces to perform their tests. You can customize the test system by subclassing the standard unit test classes. I'm not sure how you want to divide responsibilities, but maybe something like this: Python code:
|
# ? Nov 20, 2014 03:58 |
|
Jewel posted:Why did python choose that design philosophy in the first place, actually? Most other languages, at least that I use, implicitly give "this" to a function in a class, and if python forces you to have a keyword representing "this"/"self" in every member function, why not just provide it via a builtin keyword instead of making you add the argument manually every time? And then to go a step further, why not implicitly scope local arguments on the class. In C# you can type "Property = true" but you can also type "this.Property = true" if you want to avoid clashes with argument names, where "Property" will, by default, scope to be the passed in argument rather than the member of the same name. Because they chose a different design with a different set of tradeoffs. Not having self be a builtin means that you can replace it, and things like @staticmethod and @classmethod do. And at that point, it's not like you can magically add the "self" object to scope, because you don't know if it's a "self" object at all! I enjoy the Python-style explicit self, though it will seem bizarre at first. Even without that, JavaScript still opts not to put "this" on the scope, because "this" can be arbitrary, open-ended object, and having scope resolution depend on whether an object has an attribute or not is really bad. And why we got rid of "with" in almost all other languages. Ruby, instead of having "this.foo", instead has an explicit scope resolution operator, making you do "@foo". This also means that with the exception of JS's "with", scope resolution is entirely lexical, which is convenient.
|
# ? Nov 20, 2014 04:17 |
|
sund posted:Allow me to interrupt this self talk to to ask a selfish work related problem. I have a number of different modules with a bunch of unittest TestCases for an couple embedded products within them. I have a test runner script that runs various combinations of tests and constructs different test reports. Ideally, I'd like the test runner to initialize the hardware interfaces and pass them to the modules with test cases and have those test cases use those hardware interfaces to perform their tests. The answer is a test harness in cmake or another tool designed for doing non-python stuff which also runs your python code. Maybe with nose and mock stubbing out whatever common functions you need, with your nose script using argparse
|
# ? Nov 20, 2014 04:38 |
|
sund posted:Allow me to interrupt this self talk to to ask a selfish work related problem. I have a number of different modules with a bunch of unittest TestCases for an couple embedded products within them. I have a test runner script that runs various combinations of tests and constructs different test reports. Ideally, I'd like the test runner to initialize the hardware interfaces and pass them to the modules with test cases and have those test cases use those hardware interfaces to perform their tests. Are your tests actually unit tests? It might make more sense to use a higher level testing framework such as Lettuce or Robot Framework. These sorts of test frameworks often let you define setup/teardowns at the test suite level as opposed to just the test case level, so that you can initialize your hardware test harness once per test suite and then reuse it for all of your test cases.
|
# ? Nov 20, 2014 09:34 |
|
Munkeymon posted:It's more annoying to write self.property than simply property as you would in most languages with the concept of a class method, for one. (Notably, most languages doesn't include JavaScript, Ruby, Lua. Ruby has uninheritable singleton class methods, and uh, javascript is prototypical) So we're talking C# or Java here. Python actually has class methods too. Jewel posted:Most other languages, at least that I use, implicitly give "this" to a function in a class, and if python forces you to have a keyword representing "this"/"self" in every member function, why not just provide it via a builtin keyword instead of making you add the argument manually every time? It has *entirely* different semantics. It makes it hard to know to bind this as a classmethod, staticmethod, or instancemethod. Maybe we do something like JavasScript where obj.method and obj.method() have different semantics, or lua's foo.method and foo:method for binding. Instead of one way of having lookup, we have two. We'd might have to special case binding too, but maybe the descriptor protocol can be saved. quote:And then to go a step further, why not implicitly scope local arguments on the class. In C# you can tell if a property exists because it is checked at compile time. With ruby, it works on the basis that if you assign to it, it's a local variable, and if you only read from it, it's a method call. Python on the other hand has lexical scope, and global variables. Like ruby, anything not locally set is treated as an outer value, but it is lexical rather than object scoped. To do x = y in python you will have to check if it's currently being called as a method, or bound to an object, and also make a choice as to wether the outer x or the object x takes precedence. This isn't easy: say you have a function like so, and you bind it to a class, should x be self.x or the argument? code:
And this only gets you so far: Because python implicitly treats variables as local when they are assigned too, you're going to need to distinguish between x = 1 and self.x = 1. Ruby solves this by adding a @ operator (as well as a @@operator (and a few more operators help with this issue too for the other namespaces ruby has) So to get this to work while preserving python semantics, we have to introduce let x = ... or @x to work out which one is which, as we can't implicitly tell from assignment anymore, or we go full hog and implement static typing. Ruby doesn't have functions or lexical scoping either. JavaScript is the halfway point between python and ruby's this: Python requires it everywhere, Javascript doesn't need it in the function def, and ruby doesn't have it anywhere. quote:personally I think is unnecessary as the only thing it lets you do is have a local and a member variable with the same name which seems dirty). In a dynamic language what happens if I do obj.x; obj.method() where method has a local variable assignment to x? it wasn't to know that x was created by someone else. Your handwaving boils down to "Why can't I just write ruby or c# in python" and the answer is: If you want these features, you're going to have to use the languages built around them. Python is a language where the OO has been built atop of functions and containers. The meta object protocol itself is actually quite nice, and as a result doesn't depend on special builtins or features of the language as much as others. Languages built around objects and methods have these implicit self, either via static typing, or via special operators to distinguish which context you are in. You end up with crap functions. Python gets a lot of power out of the Object model, and foo.x doesn't care if it's a module, a method, an attribute or whatever. Overriding how binding works can be done at the object level with __getattr__ or __setattr__, and at the attribute level with __get__ and __set. Treating objects as containers of functions gives easier composition and simpler language rules. Ruby by comparison has one syntax for method calls, obj.foo, one syntax for module lookups, obj::foo, another for attribute lookup, @foo, another for class lookup @foo, two ways of defining a method, ordinary and singleton, classes and also modules, which compose differently. No class methods, no functions, but methods, blocks, procs, lambda, all with slightly different semantics. What you are wanting is to have a little bit of sloppy, implicit code, without having to pay the price of static typing or a plethora of operators to distinguish it. The answer is no, you can't. Stop being a babby. Jewel posted:Why did python choose that design philosophy in the first place, actually? aesthetics. I rather like em.
|
# ? Nov 20, 2014 16:07 |
|
Jewel posted:Why did python choose that design philosophy in the first place, actually? http://python-history.blogspot.co.uk/2009/02/adding-support-for-user-defined-classes.html
|
# ? Nov 20, 2014 16:11 |
|
tef posted:(Notably, most languages doesn't include JavaScript, Ruby, Lua. Ruby has uninheritable singleton class methods, and uh, javascript is prototypical) Adding C++, VB and Objective-C* you get half of the top 10 most used languages and the set that a majority of people are going to be used to. So, yeah, Python is going to have some explaining to do because the way it handles scope resolution in methods just is different from the "norm" as most people see it. Note that I'm not saying it's bad or complaining! Just pointing out that the average experienced programmer is going to see explicit self, get annoyed and assume it's some weird language wart because their home language doesn't make them do that. "concept of methods that belong to instances of classes (or 'objects in memory' if you prefer) as opposed to freestanding functions" since "concept of a class method" wasn't sufficient. *sort of: self is required to run setters/getters but not to access underlying members (for anyone like me who hasn't used it and unlike me doesn't feel like reading through the docs)
|
# ? Nov 20, 2014 17:04 |
|
PyCharm 4 is out.
Thermopyle fucked around with this message at 17:12 on Nov 20, 2014 |
# ? Nov 20, 2014 17:10 |
|
Thermopyle posted:PyCharm 4 is out. Definitely going to check out this update. The notebook integration and the NumPy array viewer sounds great. Curious if the array viewer is able to peak at pandas DataFrames (doubt it, but it would be a nice bonus).
|
# ? Nov 20, 2014 17:21 |
|
Thank you to all for your responses on testing. Definitely helped loosen the log jam in my head.
|
# ? Nov 20, 2014 18:20 |
|
Is there any way to designate which functions and classes in a module will be callable by other modules that import it? Ie exclude helper functions. I'm reading about Haskell, which has this, and realized it's something I've wanted in Python for a while: ie: code:
|
# ? Nov 20, 2014 19:38 |
|
Typically you'd use a leading underscore when naming module attributes you want to treat as "private" (they're not really private).
|
# ? Nov 20, 2014 20:20 |
|
It defaults to the underscore convention, but if you need finer-grained control there's __all__. (Apparently its intended use case is to prevent creating import chains.)
|
# ? Nov 20, 2014 20:39 |
|
Cool, rolling with a leading underscore. Apparently it prevents from module import function imports, and leaves import module unchanged. This, it being a standard convention, and PyCharm recognizing the funcs as private is good enough.
|
# ? Nov 20, 2014 23:24 |
|
vikingstrike posted:Definitely going to check out this update. The notebook integration and the NumPy array viewer sounds great. Curious if the array viewer is able to peak at pandas DataFrames (doubt it, but it would be a nice bonus). https://youtrack.jetbrains.com/issue/PY-14330
|
# ? Nov 21, 2014 00:04 |
when are they gonna fix the thing where code completion doesn't work with pygame
|
|
# ? Nov 21, 2014 01:57 |
|
I want to take the information present in a database table (.accdb) and read it into a Pandas dataframe. Is this possible? From Googling and looking on Stackoverflow I cannot find a way but I have never used a database before so maybe I am missing something.
|
# ? Nov 21, 2014 05:15 |
|
Jose Cuervo posted:I want to take the information present in a database table (.accdb) and read it into a Pandas dataframe. Is this possible? From Googling and looking on Stackoverflow I cannot find a way but I have never used a database before so maybe I am missing something.
|
# ? Nov 21, 2014 07:59 |
|
Jose Cuervo posted:I want to take the information present in a database table (.accdb) and read it into a Pandas dataframe. Is this possible? From Googling and looking on Stackoverflow I cannot find a way but I have never used a database before so maybe I am missing something.
|
# ? Nov 21, 2014 14:12 |
|
Cingulate posted:Export to a csv and pandas.read_csv? This is what I ended up using since the database does not change and so I only need to export the data once.
|
# ? Nov 21, 2014 15:07 |
|
Thanks for this! It seems like the spyder IDE already lets you peak at DataFrames, but I enjoy pycharm too much to switch. iPython notebooks are sufficient for me.
|
# ? Nov 21, 2014 16:07 |
I don't know much about coding, but I want to write a Python scraper. I want to point it to a website and have it pull a column of data from each of several PDFs linked from the page, then combine the results for each PDF into one .csv and save it with the current date. I have done some Python tutorials, but I found it hard to find a good one for scraping that was easy to use without a lot of supplemental reading and also didn't rely on old packages or libraries that I either can't find or seem to have better alternatives now. If possible I want one for Python 3 so I can get used to working with that. Does anyone know any good, modern scraping tutorials?
|
|
# ? Nov 23, 2014 22:02 |
|
Another SQLAlchemy question; I hope this one is simple but I can't find the right terms to search for the answer:Python code:
Python code:
This works: Python code:
|
# ? Nov 24, 2014 05:04 |
|
WAMPA_STOMPA posted:I don't know much about coding, but I want to write a Python scraper. I want to point it to a website and have it pull a column of data from each of several PDFs linked from the page, then combine the results for each PDF into one .csv and save it with the current date. I have done some Python tutorials, but I found it hard to find a good one for scraping that was easy to use without a lot of supplemental reading and also didn't rely on old packages or libraries that I either can't find or seem to have better alternatives now. If possible I want one for Python 3 so I can get used to working with that. Does anyone know any good, modern scraping tutorials? Use beautifulsoup to parse, requests to pull. PDF parsing is a pain, since PDFs are drawing instructions more than text. Getting a column may be unreliable depending on how the PDF is written. Pypdf2 is probably your best bet if you can't use a TeX converter or run headless libreoffice to turn it into something sane. Writing PDFs is easy to do. Reading and manipulating often isn't. Someone else may know a good library for it, though. KICK BAMA KICK posted:This works:
|
# ? Nov 24, 2014 06:34 |
|
Since I'm a bad programmer, I very often do something that's basicallycode:
code:
But imagine I'm not construction a dict, but adding to a Pandas data frame or such things. Eg., code:
E: fixed the dict comprehension Cingulate fucked around with this message at 15:08 on Nov 27, 2014 |
# ? Nov 27, 2014 12:39 |
|
I have a sneaking suspicion that upgrading to Yosemite sodded my Python installation. I may have multiple installations or some craziness. Short of re-installing Yosemite from scratch, what is the best way to nuke everything and re-install Python? I intend to restore my packages using the local pip trick, so nothing should be lost in the process.
|
# ? Nov 27, 2014 14:14 |
|
Cingulate posted:Since I'm a bad programmer, I very often do something that's basically I'd use pd.Series Python code:
Python code:
Symbolic Butt fucked around with this message at 14:53 on Nov 27, 2014 |
# ? Nov 27, 2014 14:51 |
|
Ah yes, that's a much smarter implementation for the specific Pandas case that'll actually improve about half of my scripts. But is there a general answer? What if I want to assign bar(y) to some foo specified by x without a loop: code:
|
# ? Nov 27, 2014 15:05 |
|
No no, I get what you're asking now. Basically if the data structure has a dictionary-like update method (which is not the case for pd.DataFrame) you can do something like this:Python code:
Symbolic Butt fucked around with this message at 15:30 on Nov 27, 2014 |
# ? Nov 27, 2014 15:28 |
|
Ah, knew there'd be some map-like thing in there. Thanks.
|
# ? Nov 27, 2014 15:38 |
|
|
# ? May 20, 2024 04:40 |
|
Are there any usable bluetooth libraries for Python I can use in OSX Yosemite? I've found PyBluez, which hasn't updated its Mac support in seven years, and lightblue, which apparently stopped development in 2009. There are a few workarounds on the latter I've been able to use to at least install and import from the console, but gives me an error when I try to find devices.
|
# ? Nov 28, 2014 06:18 |