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
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!

QuarkJets posted:

Think of sources as .py files. Simple enough. Sources folders are added to the PYTHONPATH so that you can import their contents. If you want to import python files from other python files, and some of those files are in directories, then you'll either need to modify your PYTHONPATH yourself or establish those directories as source folders.

Think of resources as any data that you may want to use in your project; CSS files, images, etc. Then if you try to load a file (say "data.txt" which lives deep in some subdirectory) you can use a shorter root path ("/data.txt") instead of having to specify a relative path from the root directory of your project (eg "full/path/to/some/bullshit/directory/data.txt") or a full path

So if I had a folder called "data" and I mark it as a resource directory (with the top folder marked as the root directory), then you're saying I could write in my code something like:

code:
with open('/data/test.txt', 'rb') as f:
    original_data = f.read()
But obviously this would only work if I execute the code from within Pycharm right? So if I'm running the scripts from the command line anyway it doesn't help me at all?

Adbot
ADBOT LOVES YOU

QuarkJets
Sep 8, 2008

Boris Galerkin posted:

So if I had a folder called "data" and I mark it as a resource directory (with the top folder marked as the root directory), then you're saying I could write in my code something like:

code:
with open('/data/test.txt', 'rb') as f:
    original_data = f.read()
But obviously this would only work if I execute the code from within Pycharm right? So if I'm running the scripts from the command line anyway it doesn't help me at all?

Correct, these features are only are useful from within a PyCharm session. This is a remnant of the fact that PyCharm descends from IntelliJ; in Java a developer is going to almost exclusively run code from within the IDE since there's a compilation step that needs to happen anyway, and it's easier to just let the IDE handle all of that for you. If you're running from the command line then don't even bother (but by the same note, you should probably get in the habit of running code inside of PyCharm; the PyCharm debugger is very good)

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!

QuarkJets posted:

Correct, these features are only are useful from within a PyCharm session. This is a remnant of the fact that PyCharm descends from IntelliJ; in Java a developer is going to almost exclusively run code from within the IDE since there's a compilation step that needs to happen anyway, and it's easier to just let the IDE handle all of that for you.

Got it, thanks. I tend to just use PyCharm for writing code for all of the cool features but run everything from a terminal because sometimes I gotta do this remotely through ssh/vim.

SurgicalOntologist
Jun 17, 2004

From what I understand, yes, it allows you to run stuff in PyCharm, for example via test runners. I find it useful when I'm starting a project and it hasn't matured to the point where I'm bothering to put together a setup.py and install it (at which point running pytest from the command line will work just as well). I'm not sure if the resources root comes into play outside of specific contexts in web apps. I can't imagine PyCharm would manipulate all paths in Python code to be relative to a resource root, because that would create more problems than it solves. It's more for paths in HTML templates, for example (at least I think that's the case).

QuarkJets
Sep 8, 2008

Boris Galerkin posted:

Got it, thanks. I tend to just use PyCharm for writing code for all of the cool features but run everything from a terminal because sometimes I gotta do this remotely through ssh/vim.

That's cool. I don't personally get a lot of benefit from running code in the IDE, either, so I often don't bother.

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!

QuarkJets posted:

That's cool. I don't personally get a lot of benefit from running code in the IDE, either, so I often don't bother.

ctrl-b and shift-f6 are probably the key combinations I press the most :v:

e: Also ctrl-shift-up/down for moving lines of code around, and ctrl-x for deleting a line, and ctrl-d for duplicating a line.

Boris Galerkin fucked around with this message at 07:35 on Apr 26, 2018

M. Night Skymall
Mar 22, 2012

Boris Galerkin posted:

Got it, thanks. I tend to just use PyCharm for writing code for all of the cool features but run everything from a terminal because sometimes I gotta do this remotely through ssh/vim.

PyCharm runs code remotely over ssh too. It'll auto deploy over sftp as well. You can also set up source roots etc remotely but it gets weird and I don't usually bother. Even if I'm running everything locally I still have it ssh into the windows Linux subsystem and use the interpreter there instead of setting everything up in windows.

Da Mott Man
Aug 3, 2012


M. Night Skymall posted:

PyCharm runs code remotely over ssh too. It'll auto deploy over sftp as well. You can also set up source roots etc remotely but it gets weird and I don't usually bother. Even if I'm running everything locally I still have it ssh into the windows Linux subsystem and use the interpreter there instead of setting everything up in windows.

PyCharm can also debug remotely and is by far its most godlike feature.

Thermopyle
Jul 1, 2003

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

Yes, the PyCharm remote features are amazing. You basically can't tell you're running code somewhere else.

SnatchRabbit
Feb 23, 2006

by sebmojo
I'm trying to do some port closure commands and the method I'm using is rather particular about how it formats ipv4 and ipv6 cidrs, so I need to run the same command a few times in order to make sure all the ports actually close properly. Ideally, I'd want my for loop to try a number of variations of ipv4 and ipv6 formats but if one fails I just want it to continue through the loop. Essentially, my code looks something like this:

code:
portlist = [1,2,3,4,5]

for port in portlist:
  try:
    closeport(ipv4, ipv6)
else:
  try: 
    closeport(ipv4)
  except ClientError as e: print (e)
  pass
How exactly should my for loop be constructed to try different variations of the ipv4 and ipv6 ports?

huhu
Feb 24, 2006
I don't really understand what's going on with your code, there are a few errors. I think what you're looking for though is something like (I'm phone posting)

code:

for p in ports: 
  is_port_closed = False
  while not is_port_closed:
    is_port_closed = closePort(p)
    # assuming is_port_closed returns true or false

You could also throw in a try/except in the while loop instead and toggle is_port_closed in the try block after the closePort function is called.

Errors in your code include a try without an except, you never use port variable, and an else without a for.

huhu fucked around with this message at 10:17 on Apr 27, 2018

M. Night Skymall
Mar 22, 2012

SnatchRabbit posted:

I'm trying to do some port closure commands and the method I'm using is rather particular about how it formats ipv4 and ipv6 cidrs, so I need to run the same command a few times in order to make sure all the ports actually close properly. Ideally, I'd want my for loop to try a number of variations of ipv4 and ipv6 formats but if one fails I just want it to continue through the loop. Essentially, my code looks something like this:

code:
portlist = [1,2,3,4,5]

for port in portlist:
  try:
    closeport(ipv4, ipv6)
else:
  try: 
    closeport(ipv4)
  except ClientError as e: print (e)
  pass
How exactly should my for loop be constructed to try different variations of the ipv4 and ipv6 ports?

Based on your code I guess it throws an exception if it fails to close the port, if that's the case you need to retry in your except clause.

code:
portlist = [1,2,3,4,5]

for port in portlist:
  try:
    closeport(ipv4, ipv6)
  except ClientError:
    try:
      closeport(ipv4)
    except ClientError as e:
      print(e)

You can keep nesting try/except chains until you run out of methods, if you want it to keep going through the for loop even if it fails to close a port just make sure you catch all your port closing related exceptions. Alternatively if it's ok to attempt to close an already closed port you could just put your try/excepts all in a row instead of nesting them.

Most import things are: only catch exceptions related to closing ports so that other exceptions propagate back up, but catch all port related exceptions or you won't make it through your for loop if a port fails to close. If a port failing to close should be an exception then just don't have an except clause on your last port closing try and it'll propagate the exception up like normal.

If retrying the same method is something you need to do then adding the while loop huhu is talking about is a good way to do that, you just catch the exception in the while loop, and might want to add some kind of counter so it doesn't go on forever.

Proteus Jones
Feb 28, 2013



Quick question for the thread.

MacOS is my primary and I use PyCharm. I mostly do network automation scripts to make my life a little easier for certain tasks.

I’ve recently purchased a gaming laptop and it seems like it would make a decent platform (7700HQ, 32GB, 1TB SSD) since the thing is a beast. However, my only real Windows exposure, beyond using RDP jump servers as an end user, is for gaming. Everything else (work, home, etc...) is pretty much macOS. I usually run my programs from Linux VMs, but I don’t use anything that limits them to a UNIX platform (like pexpect) anymore.

So my question is: which Python does everyone use?

Windows Python distrib
Using Python in my Ubuntu install on WSL

(Anaconda may be an option, but I imagine that may not work since I do use the scripts in a business context. But then again, not to generate revenue. I stopped using it to err on the side of caution about a year ago)

Proteus Jones fucked around with this message at 00:36 on Apr 28, 2018

Dominoes
Sep 20, 2007

Use the Windows native installer.

Thermopyle
Jul 1, 2003

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

If you run into some weird case, it's easy to switch which python interpreter you're using.

In other words, you're not committing yourself to anything, so just use the easiest to get started option...the windows installer.

vikingstrike
Sep 23, 2007

whats happening, captain
Pretty sure Anaconda is Ok for use even in commercial work

Proteus Jones
Feb 28, 2013



vikingstrike posted:

Pretty sure Anaconda is Ok for use even in commercial work

Huh. For some reason I always thought it was a free for educational/personal, but required licensing for commercial. I’ll have to look at it again.

Thermopyle posted:

If you run into some weird case, it's easy to switch which python interpreter you're using.

In other words, you're not committing yourself to anything, so just use the easiest to get started option...the windows installer.

Cripes. I must be tired, of course I can do this.

QuarkJets
Sep 8, 2008

Proteus Jones posted:

Huh. For some reason I always thought it was a free for educational/personal, but required licensing for commercial. I’ll have to look at it again.

NumbaPro used to require a license for commercial use, but in the last year or two they completely open sourced it; Numba is now BSD-2 and conda is BSD-3. Each package that you install beyond that you need to check on a per-case basis, as any of them could have a commercial-restricting license

PyCharm requires a license for commercial entities, it's like $100 and comes with extra web development features

Proteus Jones
Feb 28, 2013



QuarkJets posted:

NumbaPro used to require a license for commercial use, but in the last year or two they completely open sourced it; Numba is now BSD-2 and conda is BSD-3. Each package that you install beyond that you need to check on a per-case basis, as any of them could have a commercial-restricting license

PyCharm requires a license for commercial entities, it's like $100 and comes with extra web development features

I already pay for PyCharm, so that's sorted.

I'll have to check, but I'm 99% certain all the packages I use are open source. That's good to know since I cut my teeth with anaconda. I think from a familiarity perspective (especially since I'm not as familiar with Windows) I may try that first.

Thanks for the info all

Dominoes
Sep 20, 2007

That'll work too.The reason I recommended official over Anaconda is I prefer to avoid having multiple package management tools; using pipenv for everything is nice.

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!
I just create a new conda environment with the python interpreter I want and then use pip for python packages whenever possible, and conda for stuff like openblas.

Thermopyle
Jul 1, 2003

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

I've switched over to pipenv for everything because it takes the one thing npm does right and runs with it.

Unfortunately, the whole setup.py vs Pipfile is still as murky of a mess as requirements.txt vs setup.py is.

Dominoes
Sep 20, 2007

Thermopyle posted:

Unfortunately, the whole setup.py vs Pipfile is still as murky of a mess as requirements.txt vs setup.py is.
Thankfully, Pipfile vs requirements.txt is cleancut; the former replaces the latter. Any thoughts on why setup.py's install_requires can't be removed in leu of Pipfile? I assume that's what you're referring to. Ie, what's the limfac?

Npm-inspired tools with lockfiles like yarn, cargo, and pipenv are delightful.

Dominoes fucked around with this message at 20:13 on Apr 28, 2018

Thermopyle
Jul 1, 2003

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

I'm on mobile so it's too much of a hassle for me to find it, but there's a long issue on pipenvs issue tracker discussing it.

edit: https://github.com/pypa/pipenv/issues/209

Thermopyle fucked around with this message at 21:53 on Apr 28, 2018

SurgicalOntologist
Jun 17, 2004

Dominoes posted:

Any thoughts on why setup.py's install_requires can't be removed in leu of Pipfile?

install_requires is for abstract requirements, pipfile is for concrete requirements. The former is best practice for library code, the latter is recommended for applications.

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!
Speaking of environments

QuarkJets
Sep 8, 2008

lol nice job xkcd guy

Proteus Jones
Feb 28, 2013



Boris Galerkin posted:

Speaking of environments



I can’t stop giggling over “ANOTHER PIP??”

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!

Proteus Jones posted:

I can’t stop giggling over “ANOTHER PIP??”

I think that’s my favorite part.

Neslepaks
Sep 3, 2003

The question mark between pip and easy_install is my favourite

Dominoes
Sep 20, 2007

New Diffeq solver; bindings to a Julia library.

Haven't tried it yet, but this appears to overcome the limitations I've run into when trying to solve non-trivial systems in Scipy.integrate. Both the native Julia version, and its transparent Python bindings look nicer than any solver suite I've seen.

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!

Dominoes posted:

New Diffeq solver; bindings to a Julia library.

Haven't tried it yet, but this appears to overcome the limitations I've run into when trying to solve non-trivial systems in Scipy.integrate. Both the native Julia version, and its transparent Python bindings look nicer than any solver suite I've seen.

That's interesting. I've been suing scipy.integrate.solve_ivp a lot lately and haven't really found any limitations for what I'm trying to do. It's something to look into though for when I'm bored. Thanks!

e: I've never heard of Julia before, well other than by name. Isn't the Ju part of Jupyter short for Julia?

Malcolm XML
Aug 8, 2009

I always knew it would end like this.
The julia diffeq library is insanely good


Julia is like a Matlab successor but better

Rocko Bonaparte
Mar 12, 2002

Every day is Friday!
Is anybody else going to PyCon?

Sad Panda
Sep 22, 2004

I'm a Sad Panda.
I've decided that I need to automate a tedious task that I do on a daily basis. This involves loading up a website, logging in, depositing some money and playing a game.

Step 1. Load the website.

Python code:
import webbrowser

MYURL = "http://www.google.com"

CHROME_PATH = "open -a /Applications/Google\ Chrome.app %s"

def open_in_chrome(website):
    chrome = webbrowser.get(CHROME_PATH)

open_in_chrome(MYURL)
If I run this code (On OS X) when Chrome is not open, it opens up a blank window and then a second window with a tab open in it. How come?

I've tried it with Safari, Chrome, Firefox + Opera. Only Chrome does the double window nonsense.


edit : The logins + passwords are all saved in 1Password. If there's a Python module that'll interact nicely with 1Password that could do the first 2 steps.

Sad Panda fucked around with this message at 13:02 on May 6, 2018

Space Kablooey
May 6, 2009


I never used webbrowser before, but reading the docs, you should either:

1) Use webbrowser.open and just pass the URL, and the system will open the given URL in the default browser, or
2) If you actually need a reference to the browser controller, you just need to pass the name of the browser to webbrowser.get, and then use one of the open methods listed.

a witch
Jan 12, 2017

Rocko Bonaparte posted:

Is anybody else going to PyCon?

conferences in america :chloe:

Thermopyle
Jul 1, 2003

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

Every year I think "I'm going to PyCon next year" and then I put it off too long and never end up getting tickets.

Dominoes
Sep 20, 2007

Is it possible to override Python's built-in error handling with a third-party module import? I'd like to improve them to be more like Rust's, where it makes educated guesses about what you did wrong, and what the fix is. (eg, rather than just raise an AttributeError or NameError, point out a best guess of the attribute or variable you misspelled.)

Dominoes fucked around with this message at 03:51 on May 7, 2018

Adbot
ADBOT LOVES YOU

Rocko Bonaparte
Mar 12, 2002

Every day is Friday!

a witch posted:

conferences in america :chloe:

Okay fair enough.

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