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
baka kaba
Jul 19, 2003

PLEASE ASK ME, THE SELF-PROFESSED NO #1 PAUL CATTERMOLE FAN IN THE SOMETHING AWFUL S-CLUB 7 MEGATHREAD, TO NAME A SINGLE SONG BY HIS EXCELLENT NU-METAL SIDE PROJECT, SKUA, AND IF I CAN'T PLEASE TELL ME TO
EAT SHIT

Nippashish posted:

You'll avoid 99% of the cases this shows up if you do
code:
def main():
    # your code here

if __name__ == '__main__':
    main()
instead of writing code in the "main block" directly.

Well I could just disable the inspection too (although that probably is a nicer way to structure things huh), it was more about the 'who cares' aspect. I thought maybe there was an important reason!

Adbot
ADBOT LOVES YOU

QuarkJets
Sep 8, 2008

It's more there in case you expect that variable to hold something when it really doesn't, I think. Someone new to Python might think "I'll write a function to modify this global scope variable" without realizing that any changes made in that local scope would be lost (unless you're just changing that variable's mutable contents, of course)

Nippashish
Nov 2, 2005

Let me see you dance!

baka kaba posted:

Well I could just disable the inspection too (although that probably is a nicer way to structure things huh), it was more about the 'who cares' aspect. I thought maybe there was an important reason!

I started writing my code that way because I found that I would forget to pass variables from the main block to my functions and use them anyway without shadowing, which works perfectly well until you want to move that function to another file.

QuarkJets
Sep 8, 2008

I'll only use global scope variables if they're constants, so they have to get defined or imported at the top of the file and I don't allow their value to change. Everything else needs to be in a class or function. This seems to work pretty well

baka kaba
Jul 19, 2003

PLEASE ASK ME, THE SELF-PROFESSED NO #1 PAUL CATTERMOLE FAN IN THE SOMETHING AWFUL S-CLUB 7 MEGATHREAD, TO NAME A SINGLE SONG BY HIS EXCELLENT NU-METAL SIDE PROJECT, SKUA, AND IF I CAN'T PLEASE TELL ME TO
EAT SHIT

Yeah I tend to pass variables around instead of using global state too, which is why the b-but you already used this name warnings are annoying. Thanks for the tips anyway guys!

QuarkJets
Sep 8, 2008

baka kaba posted:

Yeah I tend to pass variables around instead of using global state too, which is why the b-but you already used this name warnings are annoying. Thanks for the tips anyway guys!

You shouldn't get those warnings if you're actually passing variables around, though. In my experience, I only get the shadowing warning if I'm re-using a variable name inside of a new function that still has access to a previously-defined variable of the same name. Technically it's not a problem, but it can lead to unexpected bugs

Nippashish
Nov 2, 2005

Let me see you dance!

baka kaba posted:

Yeah I tend to pass variables around instead of using global state too, which is why the b-but you already used this name warnings are annoying. Thanks for the tips anyway guys!

You're getting this warning because you are making global state and it is visible inside your functions. The solution is to make a scope for your global state to live in so that it isn't visible everywhere.

baka kaba
Jul 19, 2003

PLEASE ASK ME, THE SELF-PROFESSED NO #1 PAUL CATTERMOLE FAN IN THE SOMETHING AWFUL S-CLUB 7 MEGATHREAD, TO NAME A SINGLE SONG BY HIS EXCELLENT NU-METAL SIDE PROJECT, SKUA, AND IF I CAN'T PLEASE TELL ME TO
EAT SHIT

Yeah that's what I meant about the main() method being a nice idea. Generally I'm calling functions from __main__ and getting butt from function_a and passing it to function_b later, so I'm calling it butt everywhere because it makes sense to do so, in method signatures and in local variables.

So because something called butt is also accessible from the global scope (which may or may not be the same variable, I'm never accessing it so it doesn't matter) I'm getting the warnings. Having a separate main method and just keeping constants in the global scope basically clears all that up and makes things nice and safe. Is that a recommended practice? Seems like it should be, I've never run into it though

Dominoes
Sep 20, 2007

QuarkJets posted:

You shouldn't get those warnings if you're actually passing variables around, though. In my experience, I only get the shadowing warning if I'm re-using a variable name inside of a new function that still has access to a previously-defined variable of the same name. Technically it's not a problem, but it can lead to unexpected bugs
I agree - you should be able to do what you're describing without those warnings.

FoiledAgain
May 6, 2007

Something is going seriously wrong with one of my programs, and I'm getting a Windows error message that "python.exe has stopped working". I then have to restart everything, and I don't get a traceback printed out, so I can't tell where the problem is. How can I debug things in this situation? (I'm running Windows 8, Python 3.4, if that makes any difference)

Lysidas
Jul 26, 2002

John Diefenbaker is a madman who thinks he's John Diefenbaker.
Pillbug
Something's crashing in native code. You won't get a normal Python traceback from this, but the faulthandler module should be useful. It might suffice to simply add this to the top of one of the "main" Python file you're running:

Python code:
import faulthandler
faulthandler.enable()
This might (or might not) allow you to see a traceback that includes stack frames in the native code you're running. At very least it should give you a (modified) Python traceback.

FoiledAgain
May 6, 2007

Oh neat, I didn't know about faulthandler. It worked too! I was able to see the module and line number where things were crashing. Turns out the problem is with some PyQt code, nothing that I wrote myself. Thanks!

cinci zoo sniper
Mar 15, 2013




Is anyone aware of any differences between PyCharm Edu and PyCharm Community?

SurgicalOntologist
Jun 17, 2004

From their FAQ

quote:

PyCharm Edu is based on the Community Edition and comprises all of its functionality. Additionally, it installs and detects Python during installation. It has a simpler UI (adjustable in settings) and adds a new "Educational" project type.

cinci zoo sniper
Mar 15, 2013




drat, FAQ is the one place I didn't check. :eng99: Same thing then. Thanks!

spiritual bypass
Feb 19, 2008

Grimey Drawer
I'm looking at changing my future web projects from PHP to Python. The main things I'm looking for are better OOP features (namely method overloading), no serious loss in overall performance, excellent tooling for web applications, broad acceptance of standards (PEPs), a larger concern with code correctness compared to PHP, and an escape from the drama of PHP-FIG.

Python seems like the right language, yes?

Thermopyle
Jul 1, 2003

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

rt4 posted:

I'm looking at changing my future web projects from PHP to Python. The main things I'm looking for are better OOP features (namely method overloading), no serious loss in overall performance, excellent tooling for web applications, broad acceptance of standards (PEPs), a larger concern with code correctness compared to PHP, and an escape from the drama of PHP-FIG.

Python seems like the right language, yes?

Python is the perfect language for what you're looking for.

I'd recommend PyCharm for doing web applications. It's tooling for web applications is first class.

Also, there's no drama involved with anything Python-related.



(hahahahahha)

Jose Cuervo
Aug 25, 2004
I don't know how to describe this question succinctly to Google if there is a method for doing this kind of thing so perhaps someone will know of something and point me in the right direction.

I have a plot when the x-axis is divided into discrete equal-size intervals. During each interval, a process can either occur (that interval will be colored) or not occur (that interval will be blank).

The following are two example outcomes:





In the first example outcome there are 3 clusters, of length 3, 4, and 8 while in the second example outcome there are 2 clusters of length 4 and 10.

I was wondering if there is a way to mathematically describe (as a single number or set of numbers) the two example outcomes in terms of the number of clusters (time intervals where a process occurred during consecutive intervals) as well as the length of the clusters (number of consecutive time intervals during which the process occurred) so that someone could have an idea of the difference in clustering between two examples given just the two numbers or two sets of numbers.

Thoughts?

spiritual bypass
Feb 19, 2008

Grimey Drawer
Trying out a little bit of Python web stuff right now. In PHP, we use Composer to install libraries for the application, but keep only the composer.json file in version control. At deploy time, we tell Composer to install the libraries and the config file provides exactly what is needed.

How does this workflow happen in Python? Pip's requirements.txt doesn't quite seem to fit the bill. Is there something I'm missing? Things must be very different for the typical Python web application. Is there a good article describing standard practices around virtualenv library management?

Thermopyle
Jul 1, 2003

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

What exact functionality of composer are you looking to replicate in Python land?

spiritual bypass
Feb 19, 2008

Grimey Drawer
Just library management. Pip has the freeze feature which writes out all the installed libraries and their versions, but it seems a bit verbose compared to just writing out the libraries I've asked for and letting the rest get resolved as dependencies.

Seems like a good way to do things is to keep a requirements.txt and add venv/ to gitignore?

The March Hare
Oct 15, 2006

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

rt4 posted:

Just library management. Pip has the freeze feature which writes out all the installed libraries and their versions, but it seems a bit verbose compared to just writing out the libraries I've asked for and letting the rest get resolved as dependencies.

Seems like a good way to do things is to keep a requirements.txt and add venv/ to gitignore?

Just pip freeze > requirements.txt. There's no reason not to lock down the versions, and you can just pip install -r requirements.txt later.

Thermopyle
Jul 1, 2003

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

rt4 posted:

Just library management. Pip has the freeze feature which writes out all the installed libraries and their versions, but it seems a bit verbose compared to just writing out the libraries I've asked for and letting the rest get resolved as dependencies.

Seems like a good way to do things is to keep a requirements.txt and add venv/ to gitignore?

pip/setuptools does do this.

For example, you can write a requirements.txt that looks like:

code:
Django==1.10
And then do
code:
>> pip install -r requirements.txt
And pip will install Django and all of it's required packages because Django has a setup.py file as required by setuptools that specifies its dependencies.

You don't really have to worry about configuring setuptools unless you're wanting to distribute your code.

fletcher
Jun 27, 2003

ken park is my favorite movie

Cybernetic Crumb

The March Hare posted:

Just pip freeze > requirements.txt. There's no reason not to lock down the versions, and you can just pip install -r requirements.txt later.

It seems like if you just take the output of pip freeze verbatim and stuff it in your requirements.txt it's going to make it a pain in the rear end one you want to start updating library versions since all the dependencies are included in there as well.

I just do what Thermopyle suggested, my requirements.txt has only Django==1.10, I don't list the dependencies as well.

joebuddah
Jan 30, 2005
I am trying to save data to a custom name to a default location.

fn = "order + order + ".txt"
fn = "'"+"C:/"+fn+ "'"
myfile = open(fn, 'w')

I am using ignition which uses python 2.5
Using import.os didn't work any hints would be appreciated. Thanks

Dex
May 26, 2006

Quintuple x!!!

Would not escrow again.

VERY MISLEADING!

joebuddah posted:

I am trying to save data to a custom name to a default location.

fn = "order + order + ".txt"
fn = "'"+"C:/"+fn+ "'"
myfile = open(fn, 'w')

I am using ignition which uses python 2.5
Using import.os didn't work any hints would be appreciated. Thanks

which os commands were you trying? instead of C:/etc/etc, you should use / and the os.path.join function to build your path:

>>> import os
# Assuming we want /mydir or C:\mydir, leave the OS decide that
>>> target = os.path.join('/', 'mydir')
# Check if our target folder exists, if not make it.
# Do this for every 'new' folder in your target path, it won't automatically create the entire tree for you.
>>> if not os.path.exists(target):
... os.mkdir(target)
...
# Move to folder.
>>> os.chdir(target)
# Print out current working directory.
>>> print(os.getcwd())
c:\mydir

joebuddah
Jan 30, 2005
That worked thank you.

I honestly don't know what I tried. Instead of commenting out what didn't work, I just kept trying different things. Thanks again.

Dex
May 26, 2006

Quintuple x!!!

Would not escrow again.

VERY MISLEADING!

joebuddah posted:

That worked thank you.

I honestly don't know what I tried. Instead of commenting out what didn't work, I just kept trying different things. Thanks again.

one other thing from your snippet, you should use python's context managers('with' statements) for file I/O instead of remembering to try->do thing->finally close everywhere:

code:
with open('myfile.txt', 'wt') as outfile:
    outfile.write('blahblahblah')
once execution leaves the nested block, the file is automatically closed so you don't have to worry about it

baka kaba
Jul 19, 2003

PLEASE ASK ME, THE SELF-PROFESSED NO #1 PAUL CATTERMOLE FAN IN THE SOMETHING AWFUL S-CLUB 7 MEGATHREAD, TO NAME A SINGLE SONG BY HIS EXCELLENT NU-METAL SIDE PROJECT, SKUA, AND IF I CAN'T PLEASE TELL ME TO
EAT SHIT

mkdirs will make all the parent folders though!

Dex
May 26, 2006

Quintuple x!!!

Would not escrow again.

VERY MISLEADING!

baka kaba posted:

mkdirs will make all the parent folders though!

i should really read docs more often, i never knew this was a thing, ta

Dominoes
Sep 20, 2007

Hey, this is probably old news to most of y'all, but using py.test is a clear winner over the builtin unittest. No imports, classes, or inheritance required, just write functions with name starting with 'test', and use an assert statement in the func. Run with 'py.test filename.py'

Compared to builtin unittests, immediate advantages are lack of boilerplate, and not acting finicky about directory structure. I spent a while troubleshooting issues stemming from unittest being picky about relative imports; no issue with py.test.

Dominoes fucked around with this message at 20:40 on Aug 19, 2016

QuarkJets
Sep 8, 2008

Today I learned that there's a Python module with a really nice API that can build Powerpoint slides, and that it even works in *nix. It's called pptx. I'm going to give it a spin, this is something that I've wanted for awhile. I've done some automatic powerpoint generation in Matlab before using poorly-documented and somewhat bizarre Windows calls and it was a serious poo poo-show but did wind up saving some time over inserting the images by hand. Fingers crossed that this does what I want it to do, so far it looks awesome

Master_Odin
Apr 15, 2010

My spear never misses its mark...

ladies
What's the easiest way to build a cross platform application with a gui these days? Ideally I'd be able to build the thing for all platforms from my Mac and is just a single app/exe file.

I will say for GUI, I've only ever really don't stuff with web dev, but I'm working on something that could be run via coomandline or the GUI.

Master_Odin fucked around with this message at 13:13 on Aug 20, 2016

ArcticZombie
Sep 15, 2010
In Python? I'd go with something like Electron and Flask, you can use your web dev experience as a bonus too.

Your other choice is Qt, using PySide or PyQt.

Thermopyle
Jul 1, 2003

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

ArcticZombie posted:

In Python? I'd go with something like Electron and Flask, you can use your web dev experience as a bonus too.

Your other choice is Qt, using PySide or PyQt.

This type of thing is the best idea nowadays. The tools available to you to do web dev are just so much better than native GUIs.

Master_Odin
Apr 15, 2010

My spear never misses its mark...

ladies

Thermopyle posted:

This type of thing is the best idea nowadays. The tools available to you to do web dev are just so much better than native GUIs.

That's what I figured, but of course I cannot find any sort of information or tutorial on it beyond one blog post and then Hacker News people all complaining about how awful doing this would be. Found a bit on using thrust + python, but again it was mainly a hacker news piece from a few years ago where people talked about it that makes me a bit apprehensive about how exactly usable said solutions might be, though electron does seem super easy to use in conjunction with flask which is attractive as it basically just requires an additional server.js and then the rest of my process remains the same.

Suspicious Dish
Sep 24, 2011

2020 is the year of linux on the desktop, bro
Fun Shoe
Don't listen to Hacker News

OnceIWasAnOstrich
Jul 22, 2006

Are you intending to make a GUI program that will be high-end and compete partially on the basis of having a nice, smooth GUI after years of development? If so don't do the web technologies based method. Atom, the text editor which is why that framework exists has a poo poo GUI that looks superficially pretty but has all sorts of GUI-based annoyances in my opinion. On the other hand if your alternative is a hacked together Qt GUI made by one person who has never done it before, the web framework GUI you come up will be way better in every way and come together way faster.

Dominoes
Sep 20, 2007

OnceIWasAnOstrich posted:

Atom, the text editor which is why that framework exists has a poo poo GUI that looks superficially pretty but has all sorts of GUI-based annoyances in my opinion.
What, in particular?

Adbot
ADBOT LOVES YOU

OnceIWasAnOstrich
Jul 22, 2006

Dominoes posted:

What, in particular?

Are my annoyances? Honestly I can't think of that many so maybe I should take some of the vehemence back. I know it feels less responsive, especially dropdown menus, even on my 2500k. I have it installed on all my Windows computers and every time I've tried to use it seriously there is something about it that annoys me enough to go back to Notepad++. That said it's possible most of the problems I remember were just teething problems and it has been improved.

  • Locked thread