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
creatine
Jan 27, 2012




accipter posted:

You can create SVG files with matplotlib, which should work for you.

Hmm I didn't notice this before. Thanks, I'll read up on it.

Adbot
ADBOT LOVES YOU

Cockmaster
Feb 24, 2002
What are some good resources to learn GUI creation with PyQT5 and QT Designer? I'm not trying to do anything fancy, just a simple interface for a robot.

FoiledAgain
May 6, 2007

Cockmaster posted:

What are some good resources to learn GUI creation with PyQT5 and QT Designer? I'm not trying to do anything fancy, just a simple interface for a robot.

There's a quick set-up guide for basic apps here: http://zetcode.com/gui/pyqt5/firstprograms/ As a general tip, when you're searching for documentation don't look for PyQt because you'll just get the C++ docs. Instead, look for PySide, which is basically the same thing, but has docs with Python examples.

I've worked on several projects using PyQt5, so I can also answer specific questions.

nonathlon
Jul 9, 2004
And yet, somehow, now it's my fault ...
So if I was looking to make a "fast" version of some Python code (in the sense of writing the details in some lower-level language but keeping the API the same like cStringIO versus StringIO), what's the best way to go around it. Is it still using C? Use a helper library like Pyrex or Cython or Boost? Is all that Rust and Julia stuff viable?

Linear Zoetrope
Nov 28, 2011

A hero must cook
Rust or C are equally doable. For C you just compile to a DLL/so/dylib and for Rust you add the crate-type "cdylib" to the library and writing your library-level functions as #[no_mangle] pub extern "C" fn. C++ is only a little more complicated because you need to make a C wrapper over any C++ specific code. I'm not sure about how the Julia landscape looks anymore, I haven't used it in a while, but it should be doable with the right module.

Of those options, I'd probably go with Rust just because Rust generally minimizes the OS-specific bullshit you need to deal with compared to C and C++ being somewhat fractured in that sense (especially around the Unix/GNU and Windows/MSVC boundaries). But if you're not using things that have to worry about that, anything is fine. C and C++ naturally have more mature landscapes as far as compiler optimizations go, and it'll also be easier to get low-level common libraries that have done the bulk of the optimization grunt work for you like BLAS or LAPACK in C (which, well... you probably shouldn't be wrapping BLAS or LAPACK yourself in Python given the numpy landscape but still it's an example).

accipter
Sep 12, 2003

outlier posted:

So if I was looking to make a "fast" version of some Python code (in the sense of writing the details in some lower-level language but keeping the API the same like cStringIO versus StringIO), what's the best way to go around it. Is it still using C? Use a helper library like Pyrex or Cython or Boost? Is all that Rust and Julia stuff viable?

What is your fast code going to do? I haven't tried Cython yet, but I have had very good luck using Numba. The nice thing about Numba is that syntax is Python, but just compiled ahead of time.

Thermopyle
Jul 1, 2003

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

I haven't done much with it, but I found Cython very easy to get started with and got performance back to acceptable levels a few times I used it.

nonathlon
Jul 9, 2004
And yet, somehow, now it's my fault ...
Thanks all. The code is a set of clustering algorithms that need to be a lot more performant than currently. So it shouldn't need to do any OS stuff and will mainly be doing math. And it has to be done in a way that's maintainable and extensible and "tinker-able". My C is rusty but plausible, but I wanted to see if there was a "better way" and not get caught by any toolkit that initially seemed fine but turned out to be problematic.

Linear Zoetrope
Nov 28, 2011

A hero must cook

outlier posted:

Thanks all. The code is a set of clustering algorithms that need to be a lot more performant than currently. So it shouldn't need to do any OS stuff and will mainly be doing math. And it has to be done in a way that's maintainable and extensible and "tinker-able". My C is rusty but plausible, but I wanted to see if there was a "better way" and not get caught by any toolkit that initially seemed fine but turned out to be problematic.

Anything wrong with Scikit's clustering? It should've mostly done the Cython work for you.

nonathlon
Jul 9, 2004
And yet, somehow, now it's my fault ...

Linear Zoetrope posted:

Anything wrong with Scikit's clustering? It should've mostly done the Cython work for you.

We're developing a clustering algorithm, hence the need to make it perform.

Linear Zoetrope
Nov 28, 2011

A hero must cook

outlier posted:

We're developing a clustering algorithm, hence the need to make it perform.

Makes sense. Probably definitely go with C then, since if it ends up being competitive this will ease its inclusion into things like Scikit if you have a reference implementation ready to go and you don't want to force them to add extra dependencies like Julia or Rust.

Thermopyle
Jul 1, 2003

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

How have i not heard of pipenv before? It's even from the estimable Kenneth Reitz and the officially-recommended python packaging tool and I've still somehow missed it.

It seems pretty awesome and addresses a lot of the problems i have with pip, requirements.txt and the like.

a witch
Jan 12, 2017

Pipenv is lovely, and being able to specify dev dependencies reliably makes testing and deployment very painless.

I can't wait for ansible to start supporting Pipfiles as an alternative to requirements files.

Dominoes
Sep 20, 2007

Looks nice, but can't get it working due to this bug.

Ellie Crabcakes
Feb 1, 2008

Stop emailing my boyfriend Gay Crungus

Thermopyle posted:

I haven't done much with it, but I found Cython very easy to get started with and got performance back to acceptable levels a few times I used it.
Python's native interface is pretty dece.

Hughmoris
Apr 21, 2007
Let's go to the abyss!
What Python books (if any) do you all have? I'm thinking about picking up Fluent Python and Effective Python.

huhu
Feb 24, 2006

Hughmoris posted:

What Python books (if any) do you all have? I'm thinking about picking up Fluent Python and Effective Python.

Are you just starting out? Automate the Boring stuff is a great starting point.

Eela6
May 25, 2007
Shredded Hen
Fluent Python is by far my favorite Python book.

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

huhu posted:

Are you just starting out? Automate the Boring stuff is a great starting point.

Nah, I've been poking around Python for some time but I haven't really progressed from beginner -> intermediate. Automate The Boring Stuff is a great book.

Seventh Arrow
Jan 26, 2005

I'm in the same boat, but it doesn't seem like there are any books that are good for transitioning someone from beginner -> writing your own code. I think it's necessary to just start diving in and start doing projects - there are websites that have exercises with solutions, so I'm going to try doing those.

Having said all that, I decided to get "Learn Python the Hard Way," but so far the title seems to be a bit misleading since the author handholds the reader through some very newbie stuff. Guess I'll see how it goes!

Seventh Arrow fucked around with this message at 22:11 on Oct 21, 2017

Thermopyle
Jul 1, 2003

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

The only way I ever progressed from being a person who had read some programming books to someone who was making money doing cool stuff was to do projects that mattered to me....that solved problems I had. Struggling through those leveled me up more than I ever could have done by reading more books.

Seventh Arrow
Jan 26, 2005

Thermopyle posted:

The only way I ever progressed from being a person who had read some programming books to someone who was making money doing cool stuff was to do projects that mattered to me....that solved problems I had. Struggling through those leveled me up more than I ever could have done by reading more books.

Where did you look for projects?

Thermopyle
Jul 1, 2003

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

Seventh Arrow posted:

Where did you look for projects?

My life.

I solved my own problems. Or "problems".

I made a web scraper to create an RSS feed for a site that didn't have one. I created a thing that I could email commands to, to make things happen on my home server. Etc.

I just progressed to more and more complex things and when it became obvious my CS background was too weak for some project I'd go take some (online) classes or read some books.

huhu
Feb 24, 2006
Trying to build a basic website with Django or Flask is a good way to get into backend/full stack development if you're interested in that kind of thing. When you can hookup the stuff you build with Python to a website - a whole world of awesome possibilities opens up.

For Flask I recommend this tutorial -
https://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-i-hello-world
And the accompanying book.

For Django I'd recommend -
https://docs.djangoproject.com/en/1.11/intro/

Seventh Arrow
Jan 26, 2005

I can't speak for Hughmoris, but for myself I'm looking to get into data engineering so anything oriented towards ETL, batch, and data warehousing stuff is what I'm looking for. Still, I will look into the Flask and Django stuff, since it's probably not a good idea to be fussy while still learning - thanks for the links!

Vivian Darkbloom
Jul 14, 2004


I want to introduce a 7th grade student to coding, using Python. He's pretty good at math but I don't think he has any experience with programming languages. Is there any reasonably up-to-date book or curriculum for this?

Thermopyle
Jul 1, 2003

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

Think Like a Computer Scientist might be good at that level?

It's been so long since I read it, though...

Vivian Darkbloom
Jul 14, 2004


Thermopyle posted:

Think Like a Computer Scientist might be good at that level?

It's been so long since I read it, though...

That one looks pretty good. Amazon's top one is Python for Kids by Briggs; I don't know if it's any good though.

Cockmaster
Feb 24, 2002

FoiledAgain posted:

There's a quick set-up guide for basic apps here: http://zetcode.com/gui/pyqt5/firstprograms/ As a general tip, when you're searching for documentation don't look for PyQt because you'll just get the C++ docs. Instead, look for PySide, which is basically the same thing, but has docs with Python examples.

I've worked on several projects using PyQt5, so I can also answer specific questions.

Thank you. I've also noticed this: https://www.tutorialspoint.com/pyqt/

How do you display a decimal point in the QLCDNumber display? I've run the setSmallDecimalPoint(True) method, but it doesn't seem to have any effect.

Also, does anyone know where I can learn about bitwise operations in Python?

Cockmaster fucked around with this message at 13:06 on Oct 23, 2017

FoiledAgain
May 6, 2007

Cockmaster posted:

Thank you. I've also noticed this: https://www.tutorialspoint.com/pyqt/

How do you display a decimal point in the QLCDNumber display? I've run the setSmallDecimalPoint(True) method, but it doesn't seem to have any effect.

Also, does anyone know where I can learn about bitwise operations in Python?

The function setSmallDecimalPoint() doesn't change whether the decimal point is displayed, it changes *how* it's displayed. If True, then the decimal point is small and squeezed between other numbers. If False then the decimal point takes up as much space as a digit does in the display.

To get a decimal in the first place, you need to set the "number mode" to decimal, which you can do by calling setDecMode (note that this doesn't take any arguments). Try this out:

code:
lcd = QLCDNumber()
lcd.setDecMode() #sets to decimal number display
lcd.setNumDigits(12) #displays up to 12 digits, put whatever you want here
lcd.setSmallDecimalPoint(True) #try using False here to see what happens
lcd.display(312.45)

Rocko Bonaparte
Mar 12, 2002

Every day is Friday!
Are there recommended techniques for preserving an exception call stack across a larger, multithreaded application? I'm finding I'm losing information about problems particularly when catching and reprocessing exceptions that are getting shot across threads using concurrent.futures.Future objects. I'll lose the stack trace when the problem first happened.

Modulo16
Feb 12, 2014

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

Hi guys,

I'm just now diving into python for scripting, and so far I am really enjoying the language. However, I am running into an issue where I am trying to run a specific query and email each line of the results tog a specific address.

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'
        msg = (header + (str(result)))
        mailServer.sendmail(sender, mailto, msg.as_string())
        time.sleep(1)
        print ("Sent Email")
Cursor declared as cnxn.cursor from the pyodbc library

and I am using the smtplib library for email sending.

Is there a different SMTP library that works better for this type of operation? Alternatively, am I missing something with how I am preparing the information to be sent?

Thermopyle
Jul 1, 2003

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

Frank Viola posted:

Hi guys,

I'm just now diving into python for scripting, and so far I am really enjoying the language. However, I am running into an issue where I am trying to run a specific query and email each line of the results tog a specific address.

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'
        msg = (header + (str(result)))
        mailServer.sendmail(sender, mailto, msg.as_string())
        time.sleep(1)
        print ("Sent Email")
Cursor declared as cnxn.cursor from the pyodbc library

and I am using the smtplib library for email sending.

Is there a different SMTP library that works better for this type of operation? Alternatively, am I missing something with how I am preparing the information to be sent?

So what is the issue you're having?

Modulo16
Feb 12, 2014

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

Thermopyle posted:

So what is the issue you're having?

The issue is that the email get's sent with no message body containing the line from the query, but it is printed to the output in the console when I am debugging

Mr. Crow
May 22, 2008

Snap City mayor for life
I'm trying to learn unit testing in Python, I've only ever done small one-off things in it (python) so my 'pythonic' sensor is probably wrong; however looking online all the articles I see on doing mocks recommend using patch and it seems like a really bad and unmaintainable code smell.

I'm coming from from c languages where dependency injection and injecting fakes is kosher, gmock, nsubstitute, etc.

Can someone point me to a good article going over best practices with unit testing and if people really do recommend patching everywhere?

Symbolic Butt
Mar 22, 2009

(_!_)
Buglord

Frank Viola posted:

Hi guys,

I'm just now diving into python for scripting, and so far I am really enjoying the language. However, I am running into an issue where I am trying to run a specific query and email each line of the results tog a specific address.

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'
        msg = (header + (str(result)))
        mailServer.sendmail(sender, mailto, msg.as_string())
        time.sleep(1)
        print ("Sent Email")
Cursor declared as cnxn.cursor from the pyodbc library

and I am using the smtplib library for email sending.

Is there a different SMTP library that works better for this type of operation? Alternatively, am I missing something with how I am preparing the information to be sent?

Are you using the MIMEText object from email.mime.text? Because the way you're sending msg, which is a concatenation of 2 strings, instead of a MIMEText looks odd to me.

cliffy
Apr 12, 2002

Mr. Crow posted:

I'm trying to learn unit testing in Python, I've only ever done small one-off things in it (python) so my 'pythonic' sensor is probably wrong; however looking online all the articles I see on doing mocks recommend using patch and it seems like a really bad and unmaintainable code smell.

I'm coming from from c languages where dependency injection and injecting fakes is kosher, gmock, nsubstitute, etc.

Can someone point me to a good article going over best practices with unit testing and if people really do recommend patching everywhere?

The idea of a unit test is that you're testing a small piece of code in isolation. Think function level or smaller. Typically you're going to have several tests exercising a single function. Mock provides a good way to isolate or mock away unwanted side-effects of the function/code under test. If the unit you're testing has a lot of side-effects, then that might be a code smell, but not necessarily.

If you're structuring your code well, then writing unit tests is usually fairly easy. 'Structured well' meaning: functions tend to have one or no side-effects. Functions are short and focus on accomplishing one goal.

Modulo16
Feb 12, 2014

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

Symbolic Butt posted:

Are you using the MIMEText object from email.mime.text? Because the way you're sending msg, which is a concatenation of 2 strings, instead of a MIMEText looks odd to me.

I had considered using the MIMEtext library for this, I just wanted to try it this way. I will give that a try, and see if I can get the result I want.

Ellie Crabcakes
Feb 1, 2008

Stop emailing my boyfriend Gay Crungus

Frank Viola posted:

The issue is that the email get's sent with no message body containing the line from the query, but it is printed to the output in the console when I am debugging
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.

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 trying to learn unit testing in Python, I've only ever done small one-off things in it (python) so my 'pythonic' sensor is probably wrong; however looking online all the articles I see on doing mocks recommend using patch and it seems like a really bad and unmaintainable code smell.

I'm coming from from c languages where dependency injection and injecting fakes is kosher, gmock, nsubstitute, etc.

Can someone point me to a good article going over best practices with unit testing and if people really do recommend patching everywhere?

As mentioned earlier, mocking can be a code smell, but it's not necessarily a code smell.

You'll find mocks that only exist because someone wrote a 1200 line function, or mocks that mock away all the behavior you'd actually want to test.

On the other hand they can make your code and your tests clearer.

Sometimes you'll find yourself wanting to write your code in a way that only makes sense from the viewpoint of your tests. This is a good candidate to think about if mocking will help you. Maybe you can write your code in a clearer way and use a mock in your test.

Just this morning I was writing a test for a Django view someone else wrote and it had a lot of logic and db queries in it. After struggling with figuring out what I wanted to do, I extracted a lot of its logic out into multiple functions and tested each one separately and then in the test for the view I mocked the new function out.

As far as some reading goes, I'm not sure how good it is exactly, but I hear Test Driven Development With Python recommended pretty often. Link goes to page where you can read it online or it has links to buy it from amazon or wherever.

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

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