Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Locked thread
ShadowHawk
Jun 25, 2000

CERTIFIED PRE OWNED TESLA OWNER
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?

Adbot
ADBOT LOVES YOU

SurgicalOntologist
Jun 17, 2004

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.

Symbolic Butt
Mar 22, 2009

(_!_)
Buglord

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.

Symbolic Butt
Mar 22, 2009

(_!_)
Buglord

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).

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?

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.

Thermopyle
Jul 1, 2003

...the stupid are cocksure while the intelligent are full of doubt. —Bertrand Russell

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.

Munkeymon
Aug 14, 2003

Motherfucker's got an
armor-piercing crowbar! Rigoddamndicu𝜆ous.



Thermopyle posted:

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.

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.

Thermopyle
Jul 1, 2003

...the stupid are cocksure while the intelligent are full of doubt. —Bertrand Russell

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.

yippee cahier
Mar 28, 2005

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.

Jewel
May 2, 2009

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).

suffix
Jul 27, 2013

Wheeee!

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.

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.

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:
class HardwareTestCase(unittest.TestCase):
    def __init__(self, methodName, hardwareInterface):
        super().__init__(methodName)
        self.hardwareInterface = hardwareInterface


class HardwareTestLoader(unittest.TestLoader):
    def __init__(self, hardwareInterface):
        self.hardwareInterface = hardwareInterface

    def loadTestsFromTestCase(self, testCaseClass):
        if issubclass(testCaseClass, HardwareTestCase):
            testCaseNames = self.getTestCaseNames(testCaseClass)
            if not testCaseNames and hasattr(testCaseClass, 'runTest'):
                testCaseNames = ['runTest']
            loaded_suite = self.suiteClass([testCaseClass(testCaseName, self.hardwareInterface) for testCaseName in testCaseNames])
            return loaded_suite
        else:
            return super().loadTestsFromTestCase(testCaseClass)


class HardwareDoesFooTest(HardwareTestCase):
    def testFoo(self):
        self.assertEqual('foo', self.hardwareInterface.get_foo())

...

testsuite_a = HardwareTestLoader(my_hardware).loadTestsFromTestCase(HardwareDoesFooTest)

Suspicious Dish
Sep 24, 2011

2020 is the year of linux on the desktop, bro
Fun Shoe

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.

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).

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.

evol262
Nov 30, 2010
#!/usr/bin/perl

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.

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.

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

BeefofAges
Jun 5, 2004

Cry 'Havoc!', and let slip the cows of war.

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.

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.

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.

tef
May 30, 2004

-> some l-system crap ->

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:
def foo(x):
  def bar():
     return x
  return bar


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.

tef
May 30, 2004

-> some l-system crap ->

Jewel posted:

Why did python choose that design philosophy in the first place, actually?

:words: http://python-history.blogspot.co.uk/2009/02/adding-support-for-user-defined-classes.html :toot:

Munkeymon
Aug 14, 2003

Motherfucker's got an
armor-piercing crowbar! Rigoddamndicu𝜆ous.



tef posted:

(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.

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)

Thermopyle
Jul 1, 2003

...the stupid are cocksure while the intelligent are full of doubt. —Bertrand Russell

PyCharm 4 is out.

  • iPython Notebook integration
  • NumPy array viewer
  • NumPy code insight
  • matplotlib support in integrated console
  • BDD via support for behave and lettuce
  • debugger can be attached to any running python process
  • debugger works for Jinja2 templates
  • blah blah blah
  • oh also, everything from WebStorm 9

Thermopyle fucked around with this message at 17:12 on Nov 20, 2014

vikingstrike
Sep 23, 2007

whats happening, captain

Thermopyle posted:

PyCharm 4 is out.

  • iPython Notebook integration
  • NumPy array viewer
  • NumPy code insight
  • matplotlib support in integrated console
  • BDD via support for behave and lettuce
  • debugger can be attached to any running python process
  • debugger works for Jinja2 templates
  • blah blah blah
  • oh also, everything from WebStorm 9

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).

yippee cahier
Mar 28, 2005

Thank you to all for your responses on testing. Definitely helped loosen the log jam in my head.

Dominoes
Sep 20, 2007

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:
module ImportMe
( useMe
, meToo  
) where 

useMe x = True

meToo x = butNotMe x

butNotMe x = True

BeefofAges
Jun 5, 2004

Cry 'Havoc!', and let slip the cows of war.

Typically you'd use a leading underscore when naming module attributes you want to treat as "private" (they're not really private).

Crosscontaminant
Jan 18, 2007

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.)

Dominoes
Sep 20, 2007

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.

Lysidas
Jul 26, 2002

John Diefenbaker is a madman who thinks he's John Diefenbaker.
Pillbug

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

DARPA Dad
Dec 9, 2008
when are they gonna fix the thing where code completion doesn't work with pygame

Jose Cuervo
Aug 25, 2004
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.

Dominoes
Sep 20, 2007

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.
SQLAlchemy should do it. If you post details about what's in the database and what you want the dataframe to look like, I could post example code.

Cingulate
Oct 23, 2012

by Fluffdaddy

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.
Export to a csv and pandas.read_csv?

Jose Cuervo
Aug 25, 2004

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.

vikingstrike
Sep 23, 2007

whats happening, captain

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.

WAMPA_STOMPA
Oct 21, 2010
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?

KICK BAMA KICK
Mar 2, 2009

Another SQLAlchemy question; I hope this one is simple but I can't find the right terms to search for the answer:
Python code:
class Grandparent(Base):
    children = relationship('Parent')
    grandchildren = # Is there a way to do this as some SQLAlchemy property?
    
class Parent(Base):
    parent_id = Column(Integer, ForeignKey('grandparent.id')
    parent = relationship('Grandparent')
    children = relationship('Child')
        
class Child(Base):
    parent_id = Column(Integer, ForeignKey('parent.id')
    parent = relationship('Parent')
    grandparent = association_proxy('parent', 'parent', viewonly=True)
What doesn't work:
Python code:
grandchildren = association_proxy('children', 'children', viewonly=True)
That gets a list of lists -- I want that, but flattened.
This works:
Python code:
@property
def grandchildren(self):
    return [grandchild for child in self.children for grandchild in child.children]
but I was wondering if there was a more native way to do that, which I assume might be more efficient and cleaner to read than a list comprehension. An actual relationship between the Grandchild and Grandparent classes would do it, but I was looking for something view-only that just reflects the already-existing relationships on Parent.

evol262
Nov 30, 2010
#!/usr/bin/perl

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:
Python code:
@property
def grandchildren(self):
    return [grandchild for child in self.children for grandchild in child.children]
but I was wondering if there was a more native way to do that, which I assume might be more efficient and cleaner to read than a list comprehension. An actual relationship between the Grandchild and Grandparent classes would do it, but I was looking for something view-only that just reflects the already-existing relationships on Parent.
If there's not a sqlalchemy way to do it (I don't use it and can't say), the comprehension is fine and pythonic. Or use itertools.chain

Cingulate
Oct 23, 2012

by Fluffdaddy
Since I'm a bad programmer, I very often do something that's basically

code:
for item_1, item_2 in zip(list_a, list_b):
    something(item_1) = some_function(item_2)
Now if I was for example constructing a dict, I could do dict comprehension:

code:
my_dict = {item_1: some_function(item_2) for item_1, item_2 in zip(list_a, list_b)}
And that seems very nice and sensible, and also it's fast.

But imagine I'm not construction a dict, but adding to a Pandas data frame or such things. Eg.,

code:
df = pd.DataFrame()

for item_1, item_2 in zip(list_a, list_b):
    df.loc[item_1,"some_field"] = some_function(item_2)
What's the pythonic way? Map? Lambda? Goto?

E: fixed the dict comprehension

Cingulate fucked around with this message at 15:08 on Nov 27, 2014

Adverbially
Sep 17, 2013

Soiled Meat
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.

Symbolic Butt
Mar 22, 2009

(_!_)
Buglord

Cingulate posted:

Since I'm a bad programmer, I very often do something that's basically

code:
for item_1, item_2 in zip(list_a, list_b):
    something(item_1) = some_function(item_2)
Now if I was for example constructing a dict, I could do dict comprehension:

code:
my_dict = dict()

{my_dict[item_1]: some_function(item_2) for item_1, item_2 in zip(list_a, list_b)}
And that seems very nice and sensible, and also it's fast.

But imagine I'm not construction a dict, but adding to a Pandas data frame or such things. Eg.,

code:
df = pd.DataFrame()

for item_1, item_2 in zip(list_a, list_b):
    df.loc[item_1,"some_field"] = some_function(item_2)
What's the pythonic way? Map? Lambda? Goto?

I'd use pd.Series

Python code:
df = pd.DataFrame()

df['some_field'] = pd.Series((some_function(x) for x in list_b), index=list_a)
Python code:
df = pd.DataFrame({'some_field': pd.Series((some_function(x) for x in list_b), index=list_a)})

Symbolic Butt fucked around with this message at 14:53 on Nov 27, 2014

Cingulate
Oct 23, 2012

by Fluffdaddy
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:
for x, y in zip(list_1, list_2):
    foo(x) = bar(y)
Or is the question itself somehow badly asked?

Symbolic Butt
Mar 22, 2009

(_!_)
Buglord
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:
foo.update(zip(list_1, map(bar, list_2)))

Symbolic Butt fucked around with this message at 15:30 on Nov 27, 2014

Cingulate
Oct 23, 2012

by Fluffdaddy
Ah, knew there'd be some map-like thing in there. Thanks.

Adbot
ADBOT LOVES YOU

TransatlanticFoe
Mar 1, 2003

Hell Gem
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.

  • Locked thread