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.
 
  • Post
  • Reply
Mr. Crow
May 22, 2008

Snap City mayor for life
I'm not sure if my post was incoherent or what but I'm not sure how you can suggest mocking is a code smell? If you're having to create dozens of mocks then sure, but that's a very clear indication that your unit under test is doing to much and has nothing to do with the mocks themselves.

Similarly, and I'll use a web API as an example (which is convenient because in the above mentioned book after glancing that's what he's using under his mock section), how can you unit test that a barbones function making a POST is making the POST? You either actually connect to the real service or you actually connect to a test service that tournament some test data, both of which would result in a horrible unit test. The only good option you have is to use a mock that doesn't actually create a network connection and just immediately returns some known value that you assert against. Anything else and you don't actually know what you're testing. That's the canonical example but the same principle applies between between basic function calls between two objects. If you're testing method A, and method B is called at some point in method A, you don't actually care what method B is doing and unless you mock method B out to return a known value (at the time of the test) you don't even know what you're testing.

I'm definitely not new to unit testing practices or TDD, which is what I was trying to convey in a hamfisted way by name dropping popular and modern frameworks for other languages.

I'll continue to read that book though and from some searching it looks like flexmock is maybe more what I'm looking for than the built in.

Edit: and to be extra clear I was seeing a lot of people using the @patch decorator which I didn't like, as a lot of examples just had like 5+ decorators sitting on their test method and it just seemed like a mess.

Mr. Crow fucked around with this message at 20:01 on Oct 26, 2017

Adbot
ADBOT LOVES YOU

Thermopyle
Jul 1, 2003

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

Mr. Crow posted:

I'm not sure if my post was incoherent or what but I'm not sure how you can suggest mocking is a code smell

Your post itself seemed to be saying mocks were a code smell.

Your other points are exactly good times to be using mocks.

edit: oh, you were objecting to using the patch decorator. You don't have to use it as a decorator. You can use it as a function/context-manager as well. It's just a tool. You use it as makes sense.

Thermopyle fucked around with this message at 20:25 on Oct 26, 2017

cliffy
Apr 12, 2002

The point of patching with mock is to avoid unwanted side-effects the unit under test produces.

Patching is definitely not a smell in a unit test on its own. There's many cases where there's no viable alternative.

Sometimes you patch things that don't produce side-effects, but you just don't want to worry about in the context of your unit test.

Yeah, if you see a ton of patches/patch decorators maybe that's a smell, but not necessarily with the test. More likely the unit is doing too much.

Modulo16
Feb 12, 2014

"Authorities say the phony Pope can be recognized by his high-top sneakers and incredibly foul mouth."

John Big Booty posted:

Python code:
AlertResult = cursor.fetchall()
    for result in AlertResult:
        sender = 'Sender Email
        mailto = 'Recipient group'
        header = 'To:' + mailto + '\n' + 'From: ' + sender + '\n' + 'Device Alerts for' + [Some variable] + '\n\n'
        msg = (header + (str(result)))
        mailServer.sendmail(sender, mailto, msg.as_string())
        time.sleep(1)
        print ("Sent Email")
Two newlines after the last header item.

This worked. Thank you!

Dominoes
Sep 20, 2007

Scipy 1.0.0 is out. Check out the highlights listed on that page. Two standouts for me:

1: Windows wheel. This means the whole Scipy stack is installable via Pip on Windows.
2: Long-overdue ode improvement. Previously, there were two APIs: odeint, which was easy-to-use, but limited, and ode, which was powerful, but convoluted. The new api: solve_ivp, provides robust options with an odeint-like API (yet even cleaner)

Dominoes fucked around with this message at 21:39 on Oct 26, 2017

Thermopyle
Jul 1, 2003

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

Kind of surprised they didn't have a wheel out already.

I haven't had to worry about pip installing stuff on Windows in a long time because so much stuff already has a wheel.

Dominoes
Sep 20, 2007

Thermopyle posted:

Kind of surprised they didn't have a wheel out already.

I haven't had to worry about pip installing stuff on Windows in a long time because so much stuff already has a wheel.

Scipy was a glaring exception until now; it had to do with Fortran compilers/licenses. Read here for details.

Hughmoris
Apr 21, 2007
Let's go to the abyss!
Does anyone have any good examples/blogs/libraries using functional programming in Python?

Tigren
Oct 3, 2003

Hughmoris posted:

Does anyone have any good examples/blogs/libraries using functional programming in Python?

https://www.dontdoit.com

Dominoes
Sep 20, 2007

Check out Toolz.

LochNessMonster
Feb 3, 2005

I need about three fitty


What’s a good python podcast for beginners?

a witch
Jan 12, 2017


Better suggestion: don’t.

Thermopyle
Jul 1, 2003

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

Functional programming in python can be done effectively and sometimes it can be done appropriately, but generally you shouldn't go "ok I'm going to write this program functionally".

Use it when it makes sense.

Guido doesn't optimize style for writing functionally and it shows.

https://stackoverflow.com/questions/1017621/why-isnt-python-very-good-for-functional-programming

That list isn't exactly right, but it gets the idea across.

Malcolm XML
Aug 8, 2009

I always knew it would end like this.

Thermopyle posted:

Functional programming in python can be done effectively and sometimes it can be done appropriately, but generally you shouldn't go "ok I'm going to write this program functionally".

Use it when it makes sense.

Guido doesn't optimize style for writing functionally and it shows.

https://stackoverflow.com/questions/1017621/why-isnt-python-very-good-for-functional-programming

That list isn't exactly right, but it gets the idea across.

its more of a backwards thing: guido is stuck in the late 80s-90s when it comes to programming ergonomics and thus will never see any benefit in functional programming and the sycophants who make up cPython will thus never do it

The upshot is that functional programming in python is far more painful than it needs to be and so you shouldn't use it, making it more painful since no developer will optimize for it, and...

Hughmoris
Apr 21, 2007
Let's go to the abyss!

Thermopyle posted:

Functional programming in python can be done effectively and sometimes it can be done appropriately, but generally you shouldn't go "ok I'm going to write this program functionally".

Use it when it makes sense.

Guido doesn't optimize style for writing functionally and it shows.

https://stackoverflow.com/questions/1017621/why-isnt-python-very-good-for-functional-programming

That list isn't exactly right, but it gets the idea across.

Thanks for the links.

SurgicalOntologist
Jun 17, 2004

Matthew Rocklin has some great talks demonstrating the use of toolz. Check vimeo and YouTube.

mr_package
Jun 13, 2000
There's also http://coconut-lang.org

Slimchandi
May 13, 2005
That finger on your temple is the barrel of my raygun

LochNessMonster posted:

What’s a good python podcast for beginners?

I'm very much a beginner and I enjoy Talk Python to Me and Python Bytes. podcast.init is a little more in-depth but still worth a shot.

Slimchandi
May 13, 2005
That finger on your temple is the barrel of my raygun
This may be better suited to stack overflow but:

I'm trying to implement memoizing for class methods, but implementing a cache for each class instance, not each method. I also want the cache to be based a number of instance variables which aren't necessarily called as function arguments. I have a model which has lots of methods which include calls to earlier methods, so caching fails if a state variable has changed but it isn't a method argument.

Basically I would like a combination of these two bits of code:

https://stackoverflow.com/questions/33859731/how-to-memoize-method-when-one-of-its-memoized-parameters-should-be-an-instance

and this

http://code.activestate.com/recipes/577452-a-memoize-decorator-for-instance-methods/

Is this a relatively simple thing to plumb together or much more complex?

LochNessMonster
Feb 3, 2005

I need about three fitty


Slimchandi posted:

I'm very much a beginner and I enjoy Talk Python to Me and Python Bytes. podcast.init is a little more in-depth but still worth a shot.

thanks, I'll give those a shot!

Rocko Bonaparte
Mar 12, 2002

Every day is Friday!

Slimchandi posted:

I'm very much a beginner and I enjoy Talk Python to Me and Python Bytes. podcast.init is a little more in-depth but still worth a shot.

Wait are programming podcasts really a thing?

The Fool
Oct 16, 2003


Yes and there are a lot of them.

Some of them are actually good.

Ellie Crabcakes
Feb 1, 2008

Stop emailing my boyfriend Gay Crungus

Rocko Bonaparte posted:

Wait are programming podcasts really a thing?
Gotta listen to something in between episodes of Train Talk.

Boris Galerkin
Dec 17, 2011

I don't understand why I can't harass people online. Seriously, somebody please explain why I shouldn't be allowed to stalk others on social media!

Rocko Bonaparte posted:

Wait are programming podcasts really a thing?

There are literally podcasts for like every single subject, most of them follow the same “two guys talking” format. Those two python podcasts in particular talk about/introduce python packages, interviews the developers of them, etc. It’s not really about programming per say, more about the ecosystem.

They were ok for a while but got really stale and boring cause I just didn’t care about any of the packages they were talking about.

Thirsty Dog
May 31, 2007

I find listening to podcasts is a great way for me to internalize things in loads of subjects so a Python podcast aimed at newbies would be amazing for me.

Dominoes
Sep 20, 2007

Hey dudes: How do you actively test/work on functions in your code? This is a broad-question; I hope this context helps:

My workflow has involved editing files in PyCharm or another editor, and having a separate Ipyhon window open with %autoreload 2 set; I import the module. To test, I save the code in the editor, and work in Ipython.

I've recently tried Rstudio and Spyder... Love how you can just work entirely in the IDE and run bits of/all your code etc whenever you want. This doesn't appear possible in Pycharm: The run button at the top just runs the whole thing, as if it were a standalone script. The built-in console doesn't use the Spyder/Rstudio behavior I described, and doesn't work with autoreload. Is there any way to do this in Pycharm?

Dominoes fucked around with this message at 13:25 on Nov 4, 2017

vikingstrike
Sep 23, 2007

whats happening, captain
You can use the debugger to stop at a point in the code, hit the console button and interact with the ipython window there (there is also a variables viewer too). Or you can highlight whatever code you want to run, and then right click and choose “execute in python console” and it will run it in an open console (if you have one) or open a new one and execute it there.

I used to do exactly what you describe with pycharm + a terminal window but I do it all in pycharm now.

I know this is short but I’m phone posting but can provide more detail later if needed.

The March Hare
Oct 15, 2006

Je rêve d'un
Wayne's World 3
Buglord
Been a while since I've had to do this but I need to make a GUI for a Python project. Need it to work on Python 3, absolutely needs to work on Windows (better if cross platform, but not required), absolutely need to be able to put it in the system tray, and would prefer it not make me want to die while working with it.

Any advice or am I stuck choosing between tkinter and qt or whatever?

Thermopyle
Jul 1, 2003

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

The March Hare posted:

Been a while since I've had to do this but I need to make a GUI for a Python project. Need it to work on Python 3, absolutely needs to work on Windows (better if cross platform, but not required), absolutely need to be able to put it in the system tray, and would prefer it not make me want to die while working with it.

Any advice or am I stuck choosing between tkinter and qt or whatever?

Nowadays I use HTML/CSS/JS + Electron when I'm making a Python GUI.

Other than that look at PySide, Pyforms, and Kivy to see if any meet your needs.

The March Hare
Oct 15, 2006

Je rêve d'un
Wayne's World 3
Buglord

Thermopyle posted:

Nowadays I use HTML/CSS/JS + Electron when I'm making a Python GUI.

Other than that look at PySide, Pyforms, and Kivy to see if any meet your needs.

I'm down with Electron in theory but the word on the street is that even the simplest of apps incur a massive overhead. Is this actually true or have things changed a bit? I'm writing a pretty simple app, I don't want it to have a 500MB memory allocation or whatever.

Thermopyle
Jul 1, 2003

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

The March Hare posted:

I'm down with Electron in theory but the word on the street is that even the simplest of apps incur a massive overhead. Is this actually true or have things changed a bit? I'm writing a pretty simple app, I don't want it to have a 500MB memory allocation or whatever.

Personally, I don't really care how much memory my application uses. The only thing that matters is if it causes actual problems.

For what its worth, a minimal Electron app will use 50-60MB.

It's a tradeoff I'm often willing to make to be able to use the best UI design and creation paradigm.

If your app is simple I'd probably go with Pyforms (not sure if it does system tray) or PySide.

Thermopyle fucked around with this message at 19:02 on Nov 4, 2017

QuarkJets
Sep 8, 2008

The March Hare posted:

Been a while since I've had to do this but I need to make a GUI for a Python project. Need it to work on Python 3, absolutely needs to work on Windows (better if cross platform, but not required), absolutely need to be able to put it in the system tray, and would prefer it not make me want to die while working with it.

Any advice or am I stuck choosing between tkinter and qt or whatever?

Qt is pretty easy to use

The March Hare
Oct 15, 2006

Je rêve d'un
Wayne's World 3
Buglord

QuarkJets posted:

Qt is pretty easy to use

I used qt a while back and I don't remember it being difficult, but I do remember the documentation for pyqt sort of sucking. I checked out the kivy docs this afternoon and it looks totally fine, probably just going to use it and see what happens. Thanks y'all~

feedmegin
Jul 30, 2008

Thermopyle posted:

Personally, I don't really care how much memory my application uses. The only thing that matters is if it causes actual problems.

For what its worth, a minimal Electron app will use 50-60MB.

It's a tradeoff I'm often willing to make to be able to use the best UI design and creation paradigm.

If your app is simple I'd probably go with Pyforms (not sure if it does system tray) or PySide.

It's not just the RAM used though, that's all used up by doing random poo poo, with all the performance slowdown that's implied in actually filling those megabytes, especially on a lower specced machine than a developer workstation.

Thermopyle
Jul 1, 2003

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

Yes, that's what I would call "actual problems".

Thermopyle
Jul 1, 2003

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

Another gui option is to do the html/css/js route and use the webbrowser standard library module to open your interface in the user's browser.

You'd have to use another gui framework to make your system tray icon. Only you can decide if that's something reasonable for your use case.

QuarkJets
Sep 8, 2008

The March Hare posted:

I used qt a while back and I don't remember it being difficult, but I do remember the documentation for pyqt sort of sucking. I checked out the kivy docs this afternoon and it looks totally fine, probably just going to use it and see what happens. Thanks y'all~

The documentation for Qt is extensive, and pyqt is basically just a wrapper for that. Anytime that you have a question about pyqt there's a very good chance that your question is actually about qt

FoiledAgain
May 6, 2007

QuarkJets posted:

The documentation for Qt is extensive, and pyqt is basically just a wrapper for that. Anytime that you have a question about pyqt there's a very good chance that your question is actually about qt

I've mentioned this previously in the thread, but it's worth repeating here I think: if you want help with PyQt, it is useful to consult the PySide docs instead. The two packages have minor differences, but the PySide docs all give Python examples whereas the PyQt docs are almost non-existent and you frequently get redirected to C++ docs instead.

unpacked robinhood
Feb 18, 2013

by Fluffdaddy
I'd like to package a little tool I made for an outdated OSX. There are a few directories with icons, config files etc

Assuming all the imports can be resolved on the target system, Is there a clean packaging technique to distribute a single file instead of a zip that must be decompressed ? I'm on linux.

vvv thanks !

unpacked robinhood fucked around with this message at 21:51 on Nov 14, 2017

Adbot
ADBOT LOVES YOU

wolrah
May 8, 2006
what?

unpacked robinhood posted:

I'd like to package a little tool I made for an outdated OSX. There are a few directories with icons, config files etc

Assuming all the imports can be resolved on the target system, Is there a clean packaging technique to distribute a single file instead of a zip that must be decompressed ? I'm on linux.

https://py2app.readthedocs.io/en/latest/

It'll create a .app bundle, which you will still need to zip up because in reality it's just a specially named/structured folder, but that's the standard way to distribute applications on Mac OS.

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply