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
Dominoes
Sep 20, 2007

Spyder's the only editor I've found that lets you easily run individual snippets of code - something I find remarkable in conjunction with its low popularity.

Adbot
ADBOT LOVES YOU

vikingstrike
Sep 23, 2007

whats happening, captain
Option + Shift + E in PyCharm. Use it all the time.

Dominoes
Sep 20, 2007

vikingstrike posted:

Option + Shift + E in PyCharm. Use it all the time.
How, specifically, do you use this? The naive approach appears to run code without loading the rest of the module.

cinci zoo sniper
Mar 15, 2013




Dominoes posted:

Spyder's the only editor I've found that lets you easily run individual snippets of code - something I find remarkable in conjunction with its low popularity.

I run interactive console in Pycharm and just code cells there, you can delineate them with #%% in code.

Thermopyle
Jul 1, 2003

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

Dominoes posted:

Spyder's the only editor I've found that lets you easily run individual snippets of code - something I find remarkable in conjunction with its low popularity.

I...can't say I've ever really wanted to do this on some sort of priority level wherein I'd even think about adding it to a list of things my IDE needs to do. Now it's got me wondering what the difference in our workflows is that makes this A Thing.

NinpoEspiritoSanto
Oct 22, 2013




Am I the only one that never executes anything from my ide other than linting?

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

You mean just running a bit of code to check it works or to generate some output quickly? You can do that with shift+return in VS Code too

QuarkJets
Sep 8, 2008

Bundy posted:

Am I the only one that never executes anything from my ide other than linting?

Probably not, but if your ide supports it then you may as well use it cause it's useful

Dominoes
Sep 20, 2007

Thermopyle posted:

I...can't say I've ever really wanted to do this on some sort of priority level wherein I'd even think about adding it to a list of things my IDE needs to do. Now it's got me wondering what the difference in our workflows is that makes this A Thing.
Experimenting with numerical/mathematical/scientific things. Eg trying different BCs and constants on an integral and looking at the plots. A middle-ground between QtConsole and a full program. Notebook (obviously) does this too, but changes other aspects of the workflow. This is the sort of thing Python excels at.

Dominoes fucked around with this message at 22:20 on Mar 2, 2019

NinpoEspiritoSanto
Oct 22, 2013




QuarkJets posted:

Probably not, but if your ide supports it then you may as well use it cause it's useful

Eh, I've always got a term window with tabs active in my venv anyway and my users are unlikely to be running my app from their ide. I use pycharm atm and I always find the execution area to be odd. One thing I do miss from Spyder is the ipython integration, that is very tight and I did use it a lot there, but adapted to the terminal window approach after making the switch to pycharm for all the other good ide stuff it has over Spyder.

mbt
Aug 13, 2012

I spend a lot of time writing one-off scripts for work and idle is still great at that. Cmd up a virtualenv, write a hundred lines of code and convert to exe for someone else to deal with. Maybe you can do that faster in another ide but idle is 'fast enough'.

Vscode owns otherwise, though

Hadlock
Nov 9, 2004

Bundy posted:

Am I the only one that never executes anything from my ide other than linting?

Occasionally I'll use the terminal in vscode to double check something, or if it happens to be the only terminal visible, run manage.py but it's more of a convenience thing

I will, on great occasion, bust out python comand like to sanity check syntax, or troubleshoot syntax, like a reporting/SQL jockey, but max once a month

I could never use an IDE without a linter, it's especially painful to load someone else's code with a bunch of warnings + trailing whitespace garbage sprinkled everywhere.

Sad Panda
Sep 22, 2004

I'm a Sad Panda.
What can make importlib get stuck in a loop?

I'm rather confused. I've got a piece of code that uses two threads. One is for Flask and it has a dropdown menu which I use to pick tasks. When the option is selected, it runs this code..

Python code:
def get_task(task_name, credentials, log, proxy=None):
    """ Reads a task name and returns an instance of the task """
    try:
        module = importlib.import_module(f'Tasks.{task_name}')
    except Exception as e:
        print(e)
    class_ = getattr(module, task_name)
    # Creates an instance of a Task
    task = class_(credentials, log, proxy, task_name)

    return task
For most of my tasks this works just fine.
The only time it doesn't work? If the task that I'm trying to import contains either of these two lines...

Python code:
import pyautogui
from pyscreeze import ImageNotFoundException
None of the other things that I'm trying to import cause issues, but pyautogui and pyscreeze make it fail. It never throws an exception, it just seems to sit in an infinite wait and does nothing. If I throw a breakpoint on that import line, and then try to evaluate that line by itself then PyCharm eventually gives up and says

"Timeout waiting for response on 115"


I have also tried
Python code:
        module = __import__(f'Tasks.{task_name}')
And it does the same.

Sad Panda fucked around with this message at 17:04 on Mar 3, 2019

LongSack
Jan 17, 2003

I am (as an exercise) converting one of my C# programs to Python. Some of it is pretty straightforward, but in my C# program I use several static classes for things that I don't need to instantiate and i don't feel like passing around from method to method.

The program I am converting is a tool that builds software models of Firewalls from configuration files stored on a TFTP server. To speed things up (especially since more than half of my team of engineers is in India), I cache copies of the config files locally. I use a static class called FileManager to manage access to the configuration files. The manager determines whether the local files are current and if not copies current versions to the cache directory, and maintains a dictionary of files which are managed.

I'm using a singleton model to create the classes in python:
Python code:
class Singleton(type):
    instances = {}
    def __call__(cls, *args, **kwargs):
        if cls not in cls.instances:
            cls.instances[cls] = super(Singleton, cls).__call__(*args, **kwargs)
        return cls.instances[cls]
    
    @classmethod
    def clear(cls):
        cls.instances = {}

class FileManager(object, metaclass=singleton.Singleton):
...
Am I barking up the wrong tree? Before i get too far down this path (there are 13 ...Manager classes in total) - is there a better way? Am i thinking too much in C#?

Nippashish
Nov 2, 2005

Let me see you dance!

LongSack posted:

Am I barking up the wrong tree? Before i get too far down this path (there are 13 ...Manager classes in total) - is there a better way? Am i thinking too much in C#?

I'd write a bunch of modules with free functions instead of a bunch of singleton classes.

mbt
Aug 13, 2012

Nippashish posted:

I'd write a bunch of modules with free functions instead of a bunch of singleton classes.

^

if you're passing a dictionary around just do that. dictionaries and python are like peanut butter and jelly

creating a class is my last resort when I can't think of a meaningful way to organize the data

Fluue
Jan 2, 2008

Nippashish posted:

I'd write a bunch of modules with free functions instead of a bunch of singleton classes.

Jumping off of this:
I'm working on an AWS Lambda app that interfaces with a few different APIs to implement the logic. These APIs are standing in for what would typically be a database with normalized relations.

User information comes in via a webhook and I need to look up related data from different services using information from the webhook. Right now it's a tangle of making different API calls to get all the information I need because of all these different APIs I need to talk to in a single request.

E.g. design:
Python code:
my_job = Job.from_json(json_data_from_webhook)

# where interface for Job API. Where data from my_job will persist.
job_service = JobService()

# Job manager info lives in an entirely different API
user_service = UserService()
job_manager = user_service.get_manager(user=my_job.user)

#translate manager id to corresponding JobService user id 
manager_id = job_service.get_user_by_email(job_manager.email)

my_job.add_manager(manager_id)

job_service.update(my_job)
Is there some python best practice for managing this mess? Should I turn
code:
my_job
into a pseudo-ActiveRecord object and pass in services?

I've typically worked with the Django ORM and a bit of SQLAlchemy, but I don't have the luxuries of either here.

Fluue fucked around with this message at 01:32 on Mar 5, 2019

LongSack
Jan 17, 2003

Nippashish posted:

I'd write a bunch of modules with free functions instead of a bunch of singleton classes.

OK, here my newbness comes through. What is a free function? Google is not being helpful. Is it just a function in a module that’s not part of a class?

Fluue
Jan 2, 2008

LongSack posted:

OK, here my newbness comes through. What is a free function? Google is not being helpful. Is it just a function in a module that’s not part of a class?

Yep! A regular function you would import from the module.

LongSack
Jan 17, 2003

Fluue posted:

Yep! A regular function you would import from the module.

What happens if a module is imported multiple times? Like my FirewallManager module needs to reference the FileManager module to get access to the configuration files, but other modules do too. In my C# code, the guts of the FileManager is two Dictionary<string, ManagedFileInfo>, one for remote files (those on the TFTP server), and the other for local (cached) files. It’s critical that there is only one set of those dictionaries.

Gangsta Lean
Dec 3, 2001

Calm, relaxed...what could be more fulfilling?

LongSack posted:

What happens if a module is imported multiple times?
Try it and find out.

mbt
Aug 13, 2012

cool. spent 2 hours trying to bugfix and ran headfirst into an Actual Bug in 3.7.

https://github.com/boppreh/keyboard/issues/186

while i'm here, here's a quick and dirty way to add keypress filtering to any program (that works regardless of active window, much better than cv.waitkey), and also how I discovered the bug in the first place. Only an issue if you're already threading, I think, but solvable if you're willing to edit python libs.

code:
import keyboard
from threading import Thread

def callback(key):
	while True:
		keyboard.wait(key)
		print("you pressed it")
		
thread = Thread(target=callback, kwargs={"key":'z'}, daemon = True)
thread.start()

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!
Does anyone know what the proper way of annotating different return types for a function overloaded with typing.overload?

I have this for example:

code:
from typing import Optional, Tuple, Union
from typing_extensions import Literal

@overload
def foo(a: float, b: float) -> Tuple[float, float]: ...

@overload
def foo(a: float, b: Literal[None] = None) -> float: ...

def foo(a: float, b: Optional[float] = None) -> Union[float, Tuple[float, float]]:
    if b:
        return (a, b)
    return a
But the Literal[None] = None part just seems really unnecessarily verbose. If I just use b: Literal[None] then PyCharm tells me I'm missing an argument.

It's fine with me doing this as well:

code:
@overload
def foo(a: float, b: float) -> Tuple[float, float]: ...

@overload
def foo(a: float, b: Optional[float] = None) -> float: ...
But again, it seems overly verbose especially since I defined the optional argument already in the actual function.

This seems to be fine as well:

code:
@overload
def foo(a: float, b: None = None) -> float: ...
But the None = None syntax looks weird.

The mypy docs give me the impression that this is what the Literal construct is suppose to help me do, but all of the examples are for the case where b is not optional, but instead like e.g. either a float or a str.

Boris Galerkin fucked around with this message at 14:10 on Mar 11, 2019

LongSack
Jan 17, 2003

If Foo is a list of Bar, which is better to get a specific Bar from Foo?
Python code:
x = filter(lambda y: y.Name == parm, Foo)
or
Python code:
x = next(y for y in Foo if y.Name == parm, None)

mbt
Aug 13, 2012

https://stackoverflow.com/questions/3013449/list-comprehension-vs-lambda-filter

either, really. combination of personal taste and 'what else are you doing'. could also make a generator out of it (example further down)

it's worth nothing that this very similar question raises a good point:
https://stackoverflow.com/questions/7125467/find-object-in-list-that-has-attribute-equal-to-some-value-that-meets-any-condi

code:
for x in test_list:
    if x.value == value:
        print "i found it!"
        break

quote:

The naive loop-break version, is perfectly Pythonic -- it's concise, clear, and efficient.

just about anything works :angel:

QuarkJets
Sep 8, 2008

I never would have expected to see someone describe that loop-breaking example as pythonic

I mean I'm cool with it but it just makes me question (again) what "pythonic" means

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

Arguably the loop version is the most explicit and clean - loop over an iterable until you find an element that matches the predicate, then move on. Creating a generator just to call next on it once works, but it feels hacky to me

Not the first time I've thought itertools needs a first function tbh

breaks
May 12, 2001

What would you envision the difference between next and first being?

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

Typically in other languages first (or whatever) takes a predicate and an iterable and returns the first item that matches. It works like filter only it's explicitly for a single item, and you don't need to wrangle some result sequence to grab what you actually want. So you get a very simple, neat, explicit call that either returns a result or some default/missing value, or even throws an exception (depending on the implementation obv)

I mean yeah you could just write it yourself but it's a nice thing to have, and it's very common

baka kaba fucked around with this message at 01:58 on Mar 13, 2019

dougdrums
Feb 25, 2005
CLIENT REQUESTED ELECTRONIC FUNDING RECEIPT (FUNDS NOW)
Pythonic just means anything you don't personally think is bad form that has acceptable performance. It's all relative imo. PEP 20 is dumb. Anyone wnho has time to muse about these things at such high level should get back to work.

Also I believe the two forms posted above have different evaluation semantics. (iter vs. for/break)

I'd probably use filter in most cases personally, unless bar.names are not unique then next. Next tends to be an alias for "first" in my head.

dougdrums fucked around with this message at 03:09 on Mar 13, 2019

Rocko Bonaparte
Mar 12, 2002

Every day is Friday!
I was using futurizer to try to migrate some code from Python 2.7 to 2.7-3.6 compatibility. Something that screwed things up was a bunch of imports for builtins.str that it inserted:

code:
from builtins import str
At one point, I was doing some decisions on some JSON. A node could be a dictionary, list, or a string. My check against the string always failed and it had something to do with that str. I tried "type(foo) is str", "issubclass," and "isinstance," and no love. I didn't start working until I made the function that produced the string rigidly case to that special str type--which I had to alias in the import to avoid a collision with str. What is the whole idea behind that particular import?

I understand in Python 3 that string handling has gotten a little bit fussier due to a more thorough go at Unicode support, but this doesn't appear to be about that at first glance.

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!
Is there a click equivalent to argparse's "add_argument_group" feature for documenting the --help text?

With argparse I can add argument groups to get optional arguments to show up in separate headers:

code:
$ foo --help

usage foo ...

foo flags:
  --bar    help text
  --baz    help text

optional arguments:
  -h, --help
With click it just puts it all under the same optional header

code:
$ foo --help

usage foo ...

optional arguments:
  --bar    help text
  --baz    help text
  -h, --help

Hadlock
Nov 9, 2004

Having a weird Django problem. Appears to be intermittent, spent several hours trying to track it down this week and last.

Followed the django girls blog tutorial as a model, has been working great for months. When I create a "blog post" record it gets a model.active=true, and then my main index page shows a summary view of each blog post, where each model has model active=True.

I have an edit, and a couple of other pages that modify the "blog post" record, those all use standard views. You post (POST) a form to the view, and then it redirects you to either the (GET) full blog post/record view, or the main index page, depending. Works great, 200s all day everyday. POST, GET. 200. POST, GET. 200. POST, GET. 200.

Then I have a delete page... This works intermittently. Most of the time you save the form and in the background it sets model.active=False and also calls a function that runs custom code. So you POST the form to the delete view, it sets False and runs custom code... Then it is supposed to send you to do a GET of the main index page... But instead the django log is showing a second POST and debug logging is showing the custom code function (and indeed, the entire if post, then run this portion of the view) getting called a second time. Since there is at least one non-idempotent thing in that function, it throws an error and returns a 500 to the end user, even though the code ran correctly the first time on the first POST. POST, POST. 500. POST, POST. 500 etc

I traced the call in Chrome developer mode, it is not posting twice. POST only happens once from the client side. As far as I can tell, this second POST I am seeing in the logs is happening from inside Django and it's not a user double clicking on the post button. I've also verified the urls.py looks good and there's no obvious call backs to the view from inside the function. Also pylint is reporting no errors. And this is the only view exhibiting the double post behavior. I even rebuilt the view from scratch and I'm still seeing the behavior.

What's super weird is that my version 0.6.46 magically started working fine (single POST, 200) last night around 3am, so I crowned it 0.7.0 and moved forward with fixing other problems, got up to 0.7.6 and then retested the delete function and it had stopped working again.

Rolled back to the 0.6.46 container (EXACT same code, no new migrations added) and it was still seeing the double POST/500 error

ha;lp

Hadlock fucked around with this message at 18:27 on Mar 14, 2019

Thermopyle
Jul 1, 2003

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

Hadlock posted:

Django.

ha;lp

There's a Django thread.

I don't have time or energy to really read or think about your post right now, but maybe someone there will.

hhhmmm
Jan 1, 2006
...?

pmchem posted:

Spyder is a really great, open source / free, lightweight, python-native piece of software that needs more appreciation in the community. I love it.

We had a push towards pycharm from the engineering team, due to the support for refactoring, super easy venv management and pep8 warnings. Did support it for a while.

But for data science, Spyder really is king. Super fast prototyping, no manual reloads of user defined libraries. In Spyder I feel like I never need to reset console/reload data. And for data science, reloading data is always a major hassle. Pycharm kinda hates that workflow even with scientific mode. Pycharm might be better if I'm building an application meant to last. But for a kaggle competition or a quick data analysis, Spyder is faster.

cinci zoo sniper
Mar 15, 2013




I don’t have any PyCharm problems working as data scientist, but then again I’m quite used to it and never really in a hurry like implied by a Kathleen competition or similar. I’ve tried trying Spyder a bunch of times, but I guess it’s just not for me.

LongSack
Jan 17, 2003

I’m discovering that I’m not a real fan of significant white space. Using Spyder makes it easy, but explicit block openers and closers would prevent this bug:
Python code:
if not key in dict:
    dict[key] = []
    dict[key].append(foo)

KICK BAMA KICK
Mar 2, 2009

LongSack posted:

I’m discovering that I’m not a real fan of significant white space. Using Spyder makes it easy, but explicit block openers and closers would prevent this bug:
Python code:
if not key in dict:
    dict[key] = []
    dict[key].append(foo)
Fair enough on the whitespace but if you find yourself doing that thing with a dict of lists there's a few options with some built-in stuff intended just for that (question unrelated but the linked answer is relevant.)

mbt
Aug 13, 2012

Is there any way to emulate/force the windows copy function without pressing ctrl c? As in not even using keyboard or pyautogui to press them virtually

Adbot
ADBOT LOVES YOU

Frequent Handies
Nov 26, 2006

      :yum:

Meyers-Briggs Testicle posted:

Is there any way to emulate/force the windows copy function without pressing ctrl c? As in not even using keyboard or pyautogui to press them virtually

There's a good answer on Stackoverflow using pywin32clipboard and another answer lower down using ctypes that doesn't require additional imports. I used the first answer ages ago to do some clipboard work.

I have a synchronous REST convenience wrapper for one of our internal systems that started out to make my life easier and turned into an sdk that's now used in a variety of things, some of which I have no idea about. I'm putting together a service that will heavily use this, and I'd like to use the starlette based FastAPI, but I'm not sure how to go about making an async version of the same package while leaving the sync functionality as it is. I've looked at a few things, like detecting whether or not the caller is sync/async and branching from there but has anyone else encountered this? I'm just not sure how to begin thinking about this, and I'm not necessarily aware of anything else that's a both-at-once kind of a deal.

Edit: Or just any better way to approach this in general.

Frequent Handies fucked around with this message at 03:00 on Mar 19, 2019

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