Author Topic: Anyone use Python?  (Read 15811 times)

JaWiB

  • definitelys definately no MacGyver
  • Jackass V
  • Posts: 1443
  • Karma: +57/-4
Anyone use Python?
« on: February 15, 2012, 07:11:56 PM »
The guys in my lab like to use python for a lot of the projects we do, so I thought I'd give it a whirl. Some of the packages are pretty handy and easy to use, so it seems like a decent choice. Problem* is, they don't write OO code and I'd much rather use classes for a lot of the stuff we're doing. For instance, we use Python code to control various instruments some of which use GPIB for communication and some of which use other methods, like RS232. So I would envision, for example, having a generic class for a temperature controller which would have methods like GetTemp and SetTemp.

In C++, I'd probably define some sort of abstract base class and then code specific classes inheriting from it for each model of temperature controller we use (we do in fact have at least one that uses serial and one that uses GPIB). But in Python, I've just learned, you have "Duck Typing" which seems to be an alternative to using inheritance. My personal opinion is that it seems like sloppy coding, but is it really just common practice to use duck typing?

*It's actually not really a problem since all of our coding projects are small and we could get away with a lot of bad practices, but I like to do things right :D

Mike

  • Jackass In Charge
  • Posts: 11248
  • Karma: +168/-32
  • Ex Asshole - a better and more caring person.
Re: Anyone use Python?
« Reply #1 on: February 15, 2012, 07:40:58 PM »
I don't but I keep meaning to get competent.  I still maintain that using indention for block level control is just fucking stupid.

JaWiB

  • definitelys definately no MacGyver
  • Jackass V
  • Posts: 1443
  • Karma: +57/-4
Re: Anyone use Python?
« Reply #2 on: February 15, 2012, 07:58:04 PM »
Yeah, I miss my curly braces. And semicolons.

Perspective

  • badfish
  • Jackass In Charge
  • Posts: 4635
  • Karma: +64/-22
    • http://jeff.bagu.org
Re: Anyone use Python?
« Reply #3 on: February 15, 2012, 08:52:59 PM »
I use python a lot. Mostly for small things though, basically any time I need to process text. It's really handy for manipulating and transforming strings.

For any larger data intensive things I use Java.

-KEN-

  • Some Other Mofo
  • Jackass In Charge
  • Posts: 1001
  • Karma: +75/-100
Re: Anyone use Python?
« Reply #4 on: February 16, 2012, 02:10:32 AM »
I use Python all day.

Duck typing won't stop you from creating base classes. That would be awful.

Code: [Select]
class BaseTempController(object):
    """
    Base class for temperature controllers
    """
    def __init__(self, temp=None):
        self._temp = temp

    def get_temp(self):
        return self._temp

    def set_temp(self, temp):
        self._temp = temp

class TempControllerA(BaseTempController):
    """
    For whatever reason, TempControllerA is evil and always
    reports the temperature 3 degrees colder than it really is.
    """
    def get_temp(self):
        """
        Lie to the people.
        """
        return self._temp - 3
   

If you want to be fancy, you can make get_temp() a property called temp, and then you could just do controller.temp instead of get_temp():

Code: [Select]
...snip...
    @property
    def temp(self):
        return self._temp - 3
...
controller = TempControllerA(45)
controller.temp    #temp is a read-only property now.

ober

  • Ashton Shagger
  • Ass Wipe
  • Posts: 14305
  • Karma: +73/-790
  • mini-ober is taking over
    • Windy Hill Web Solutions
Re: Anyone use Python?
« Reply #5 on: February 16, 2012, 09:08:11 AM »
That looks fucking awful.  I'll stick to PHP and Java.

micah

  • A real person, on the Internet.
  • Ass Wipe
  • Posts: 6915
  • Karma: +58/-55
  • Truth cannot contradict truth.
    • micahj.com
Re: Anyone use Python?
« Reply #6 on: February 16, 2012, 09:15:15 AM »
The guys in my lab like to use python... Problem is, they don't write OO code and I'd much rather use classes for a lot of the stuff we're doing.

Do they work in other OO languages, 'cuz perhaps they just don't understand it enough to get the benefits.  Coming from an inline PHP background I was really resistant to OOP a few years ago... I'm sure there are plenty of threads on here even about me whining about it.  In the end, I found myself in a immersive OOP environment and was forced to just learn it.  It didn't take long for the concept to finally click.  Now I can't imaging working without it.

Also,

"I possess a device, in my pocket, that is capable of accessing the entirety of information known to man.  I use it to look at pictures of cats and get in arguments with strangers."

-KEN-

  • Some Other Mofo
  • Jackass In Charge
  • Posts: 1001
  • Karma: +75/-100
Re: Anyone use Python?
« Reply #7 on: February 16, 2012, 12:01:03 PM »
That looks fucking awful.  I'll stick to PHP and Java.

Python is so much more readable and sane than PHP. I think objectively at this point. Stockholm Syndrome.

I guess PHP classes aren't the most awful thing I've ever seen, but I wouldn't call them pretty -- http://www.php.net/manual/en/language.oop5.basic.php

JaWiB

  • definitelys definately no MacGyver
  • Jackass V
  • Posts: 1443
  • Karma: +57/-4
Re: Anyone use Python?
« Reply #8 on: February 16, 2012, 12:51:46 PM »
If you want to be fancy, you can make get_temp() a property called temp, and then you could just do controller.temp instead of get_temp():
Sweet, didn't know about properties.

Do they work in other OO languages, 'cuz perhaps they just don't understand it enough to get the benefits.  Coming from an inline PHP background I was really resistant to OOP a few years ago... I'm sure there are plenty of threads on here even about me whining about it.  In the end, I found myself in a immersive OOP environment and was forced to just learn it.  It didn't take long for the concept to finally click.  Now I can't imaging working without it.
I don't really know. Probably not. Some of the ancient code in the lab was written in C++ and they used classes, but I don't think the guys who are here now wrote any of it. And even that code was sort of procedural code with classes

-KEN-

  • Some Other Mofo
  • Jackass In Charge
  • Posts: 1001
  • Karma: +75/-100
Re: Anyone use Python?
« Reply #9 on: February 16, 2012, 12:53:22 PM »
I should note you could write everything more succinctly this way (this is from an interactive REPL session, hence the dots and >>).

Code: [Select]
>>> class BaseTempController:
...   def __init__(self, temp=None):
...     self.temp = temp
...
>>> a = BaseTempController(45)
>>> a.temp
45
>>> class TempControllerOfLies(BaseTempController):
...   def get_temp(self):
...     return self.temp - 3
...
>>> a = TempControllerOfLies(45)
>>> a.get_temp()
42

But given what JaWiB was trying to do, I went for verbosity in the base class, and made the temp variable private.


Mike

  • Jackass In Charge
  • Posts: 11248
  • Karma: +168/-32
  • Ex Asshole - a better and more caring person.
Re: Anyone use Python?
« Reply #10 on: February 16, 2012, 12:58:46 PM »
-Ken- do you know of any good python libraries that let me make some simple GUIs?  Need to expand some command line tools to do some file selection and such.  Mainly checkboxes, radio lists, and buttons.

-KEN-

  • Some Other Mofo
  • Jackass In Charge
  • Posts: 1001
  • Karma: +75/-100
Re: Anyone use Python?
« Reply #11 on: February 16, 2012, 12:59:00 PM »
Sweet, didn't know about properties.

Yeah, I'm not a huge fan of getters and setters in my private code, but if you're writing an API or a library, it's useful to let people think they're just accessing the variable directly. Pretty useful for things like this too:

Code: [Select]
class Person:
  def __init__(self, first_name, last_name):
    self.first_name = first_name
    self.last_name = last_name

  @property
  def name(self):
    return "%s %s" % (self.first_name, self.last_name)

That way you can:

me = Person("Joe", "Schmoe")
print me.name #"Joe Schmoe"

JaWiB

  • definitelys definately no MacGyver
  • Jackass V
  • Posts: 1443
  • Karma: +57/-4
Re: Anyone use Python?
« Reply #12 on: February 16, 2012, 01:42:10 PM »
I find it funny how Python makes you use self.variable_name. It's like having to write this->variable_name all the time, which would make people laugh at you in C++

-KEN-

  • Some Other Mofo
  • Jackass In Charge
  • Posts: 1001
  • Karma: +75/-100
Re: Anyone use Python?
« Reply #13 on: February 16, 2012, 01:42:32 PM »
-Ken- do you know of any good python libraries that let me make some simple GUIs?  Need to expand some command line tools to do some file selection and such.  Mainly checkboxes, radio lists, and buttons.

I toyed with the wx library once upon a time, but I'm not qualified to make a recommendation one way or the other. Unless I'm toying around on the iPhone, I either write CLI or HTML. Not to sound too trendy, but TileMill is written in Node and HTML, and I would bet most people think it's a native application. http://mapbox.com/tilemill/

Perhaps you could just write a simple, self-contained HTML-based frontend.

-KEN-

  • Some Other Mofo
  • Jackass In Charge
  • Posts: 1001
  • Karma: +75/-100
Re: Anyone use Python?
« Reply #14 on: February 16, 2012, 01:43:46 PM »
I find it funny how Python makes you use self.variable_name. It's like having to write this->variable_name all the time, which would make people laugh at you in C++

Yeah, it is definitely annoying. Much has been written about 'self' in Python (both for and against, but mostly against I would imagine), but in the end it's really only a minor annoyance.