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
The March Hare
Oct 15, 2006

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

Thermopyle posted:

I just want to make sure that I'm not missing something...

There's no way in PySide/PyQT to set global hotkey shortcuts? By global, I mean hotkeys that trigger even when your GUI doesn't have focus.

I can't find a way to do it from PySide, and the other two things I find don't work for my needs. PyHK has an incompatible license, and PyGS hasn't been updated in 4 years and doesn't work or compile.

I'm at a tipping point where I'm going to either wire up some monstrosity between AutoIT and my GUI or scrap this project.

I tried to figure this out for months and I also could not for the life of me figure it out. It gets even worse when you start trying to do it cross platform. You can really easily hook the keyboard in Windows, but it is inelegant and isn't a viable solution for Linux/OSX.

If anyone else has an answer to this, I don't even care what it is, I'd be really curious to hear it.

Adbot
ADBOT LOVES YOU

the
Jul 18, 2004

by Cowcaster
How can I do this in one line:

Python code:
In [25]: for i in range(0,16,1): print ads[i][0]
6453
2800
1653
2849
1653
3363
3596
3922
1821
2849
7569
3610
3610
8680
1395
3160
I tried ads[:][0] but that doesn't work

Hammerite
Mar 9, 2007

And you don't remember what I said here, either, but it was pompous and stupid.
Jade Ear Joe

the posted:

How can I do this in one line:

Python code:
In [25]: for i in range(0,16,1): print ads[i][0]
6453
2800
1653
2849
1653
3363
3596
3922
1821
2849
7569
3610
3610
8680
1395
3160
I tried ads[:][0] but that doesn't work

You already did do it in one line, though...?

quote:

Python 3.3.1 (v3.3.1:d9893d13c628, Apr 6 2013, 20:25:12) [MSC v.1600 32 bit (In
tel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> x = [[1], [3], [6], [10], [15], [21], [28], [36], [45], [55]]
>>> import operator
>>> print(*list(map(operator.itemgetter(0), x)), sep = '\n')
1
3
6
10
15
21
28
36
45
55
>>>

Add from __future__ import print_function if on Python 2

Symbolic Butt
Mar 22, 2009

(_!_)
Buglord

the posted:

How can I do this in one line:

Python code:
In [25]: for i in range(0,16,1): print ads[i][0]
6453
2800
1653
2849
1653
3363
3596
3922
1821
2849
7569
3610
3610
8680
1395
3160
I tried ads[:][0] but that doesn't work

I guess you want a comprehension:

Python code:
[ads[i][0] for i in range(16)]
If 16 is the length of ads and not some specific value, this would be better:

Python code:
[element[0] for element in ads]
If you want a oneliner specifically for printing then something similar to what Hammerite did would be this:

Python code:
from __future__ import print_function

print(*[ads[i][0] for i in range(16)], sep='\n')
The * is unpacking the list to use as arguments in the print function. print is only a function in python3 so this is why we need to "import from the future" if you're on python 2. You can't do this kind of oneliner with print as a statement.

(And of course the more correct way is to use a generator expression there instead of a list comprehension)

Nimrod
Sep 20, 2003

The March Hare posted:

I tried to figure this out for months and I also could not for the life of me figure it out. It gets even worse when you start trying to do it cross platform. You can really easily hook the keyboard in Windows, but it is inelegant and isn't a viable solution for Linux/OSX.

If anyone else has an answer to this, I don't even care what it is, I'd be really curious to hear it.

Could you run some sort of daemon process in the background that picks up the hotkey presses, and sends them on to your program?

Munkeymon
Aug 14, 2003

Motherfucker's got an
armor-piercing crowbar! Rigoddamndicu𝜆ous.



ohgodwhat posted:

I've never had an issue with pd's sorts, which isn't to say it can't happen, I'm just curious what you're doing that you're seeing a problem.

I didn't think there were NaNs in the data but there were, basically. Being new to Pandas, I thought the fancy slicing notation like s[(s > 0) && (s < whatever)] was the same as .clip(0, whatever). Turns out the first filters out NaNs and the second doesn't. I did accidentally find an inconsistency between the behavior of .sort and .order, so hooray for stupid newbies, I guess?

Thermopyle
Jul 1, 2003

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

Nimrod posted:

Could you run some sort of daemon process in the background that picks up the hotkey presses, and sends them on to your program?

This is what I'm going to end up doing I think.

It just kind of sucks that PySide doesn't have the hooks built-in. I'm not exactly familiar with wxPython, but I believe it can do this with RegisterHotKey.

Suspicious Dish
Sep 24, 2011

2020 is the year of linux on the desktop, bro
Fun Shoe

Thermopyle posted:

I just want to make sure that I'm not missing something...

There's no way in PySide/PyQT to set global hotkey shortcuts? By global, I mean hotkeys that trigger even when your GUI doesn't have focus.

There's no way to do that in Qt in general. It's not a binding issue. It's not something toolkit authors or desktop environments want to expose directly, because there's the great question of "what happens if two apps do this". Under X11 you abuse the key grab system with XGrabKey. Under Windows it's RegisterHotKey. Under OS X you have to use Carbon, the old UI toolkit, because they removed the feature entirely from Cocoa. And that's a giant mess, because Carbon has its own event loop which you have to spin independently. And it won't work on new windowing systems like Wayland or Mir, which don't provide any functionality for that at all.

Really, global keyboard shortcuts belong to the user, which means that the user should configure his own settings from the desktop to trigger your app.

BigRedDot
Mar 6, 2008

the posted:

How can I do this in one line:

Python code:
In [25]: for i in range(0,16,1): print ads[i][0]
6453
2800
1653
2849
1653
3363
3596
3922
1821
2849
7569
3610
3610
8680
1395
3160
I tried ads[:][0] but that doesn't work

use numpy: ads[0:16, 0]

Edit: or ads[:, 0] if 0 through 16 is all of the rows

BigRedDot fucked around with this message at 01:44 on Jun 10, 2014

the
Jul 18, 2004

by Cowcaster

Symbolic Butt posted:

I guess you want a comprehension:


I was wondering if there was a way to print all the 16 elements in ads[1][0] through ads[16][0] with array notation, I thought that adding a : somewhere would do it, but I guess not :(

BigRedDot
Mar 6, 2008

the posted:

I was wondering if there was a way to print all the 16 elements in ads[1][0] through ads[16][0] with array notation, I thought that adding a : somewhere would do it, but I guess not :(

That works if you use a numpy array.

The March Hare
Oct 15, 2006

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

Nimrod posted:

Could you run some sort of daemon process in the background that picks up the hotkey presses, and sends them on to your program?

I could yes but I am Too Dumb to know how that would be done. I imagine I'd have to write it in something lower level?

I've been wanting to try Go, can Go monitor like that?

Thermopyle
Jul 1, 2003

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

The March Hare posted:

I could yes but I am Too Dumb to know how that would be done. I imagine I'd have to write it in something lower level?

I've been wanting to try Go, can Go monitor like that?

Personally, I'm going to use AutoIT because it's easy to do hotkeys with it. It's a decent windows scripting/automation language...you might want to pick that up.


Suspicious Dish posted:

Really, global keyboard shortcuts belong to the user, which means that the user should configure his own settings from the desktop to trigger your app.

Yeah, I've thought that for years. It'd be nice if there was some API and central place an application could register itself as wanting hotkeys for functions X, Y, and Z and there'd be a centralized UI for the user to manage it.

Unfortunately, Windows has evolved to the state where every application has them buried it it's own settings somewhere.

The March Hare
Oct 15, 2006

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

Thermopyle posted:

Personally, I'm going to use AutoIT because it's easy to do hotkeys with it. It's a decent windows scripting/automation language...you might want to pick that up.

I've used autoit, but I'm working fully cross-platform. I did some looking around and it looks like I can probably use Go for it, might just give it a...

Suspicious Dish
Sep 24, 2011

2020 is the year of linux on the desktop, bro
Fun Shoe

Thermopyle posted:

Yeah, I've thought that for years. It'd be nice if there was some API and central place an application could register itself as wanting hotkeys for functions X, Y, and Z and there'd be a centralized UI for the user to manage it.

Unfortunately, Windows has evolved to the state where every application has them buried it it's own settings somewhere.

That's basically the thing. Windows (and X11 and classic Mac OS) have historically always provided the developer with way too much control, "in case you might need it", and they've never backed down from it.

Malcolm XML
Aug 8, 2009

I always knew it would end like this.

Suspicious Dish posted:

That's basically the thing. Windows (and X11 and classic Mac OS) have historically always provided the developer with way too much control, "in case you might need it", and they've never backed down from it.

They can't due to backwards compat.

Winrt and ios is a full redesign and gives devs very little control comparatively

the
Jul 18, 2004

by Cowcaster
edit: fixed it!

the fucked around with this message at 18:07 on Jun 10, 2014

Suspicious Dish
Sep 24, 2011

2020 is the year of linux on the desktop, bro
Fun Shoe

Malcolm XML posted:

They can't due to backwards compat.

They can provide better interfaces for desktop developers by deprecating the old APIs and supplying better ones. For games, they could supply a descriptive interface for putting the system into "game mode" which possibly disables the Windows key and Alt-Tab (according to user settings), sets resolution and full-screen mode (according to user settings), enables raw input and disables the screensaver and other such things. Since the current DX APIs are loving terrible about being too low-level, you have a minute or so on every game while they modeset every possible monitor 20 times and then Windows's heuristics kick in that it's a game and sets everything up correctly

Game developers would switch over to this immediately, since now they don't have to rewrite and QA the work of disabling all the shortcuts, modesetting, etc. They don't have to lose backcompat for this, too.

Unfortunately, the Windows team will never do this.

Malcolm XML
Aug 8, 2009

I always knew it would end like this.

Suspicious Dish posted:

They can provide better interfaces for desktop developers by deprecating the old APIs and supplying better ones. For games, they could supply a descriptive interface for putting the system into "game mode" which possibly disables the Windows key and Alt-Tab (according to user settings), sets resolution and full-screen mode (according to user settings), enables raw input and disables the screensaver and other such things. Since the current DX APIs are loving terrible about being too low-level, you have a minute or so on every game while they modeset every possible monitor 20 times and then Windows's heuristics kick in that it's a game and sets everything up correctly

Game developers would switch over to this immediately, since now they don't have to rewrite and QA the work of disabling all the shortcuts, modesetting, etc. They don't have to lose backcompat for this, too.

Unfortunately, the Windows team will never do this.

Let me tell you about WinRT,

Suspicious Dish
Sep 24, 2011

2020 is the year of linux on the desktop, bro
Fun Shoe

Malcolm XML posted:

Let me tell you about WinRT,

WinRT doesn't really do anything other than add another set of low-level APIs to Windows that MS will deprecate and forget about in two years. There isn't any attempt at solving high-level problems, only the latest replacement for OLE/COM/COM+/DCOM/ATL/MFC/.NET/WPF/Silverlight/XAML.

Tacos Al Pastor
Jun 20, 2003

Just wondering how I print out a list of objects. Each object contains the name of a dog and the breed. This is what I have so far but I'm a little confused why I am overwriting things in the list the next time I append an object to the list.

Python code:
dogs = []                                 #empty dog list in which to store objects

class Dog:
    def __init__(self = None, name = None, breed = None):
        self.name = name
        self.breed = breed
        
    def print_dogs(self, name, breed):
        dogs.append(first)
        print("Dogs")
        for items in dogs:
            print(name, ":", breed)          
        
      
if __name__ == "__main__":
    while True:
        name = input("Name: ")
        breed = input("Breed: ")
        first = Dog(name, breed)
        first.print_dogs(name, breed)

Is there some string manipulation that I have to perform on each object in the list to get it to print correctly?

Any help is appreciated :)

Tacos Al Pastor fucked around with this message at 02:26 on Jun 11, 2014

SurgicalOntologist
Jun 17, 2004

The magic method __str__ determines how the object prints:

Python code:
def __str__(self):
    return '{}: {}'.format(self.name, self.breed)
Then you can just do print(first) and it will look nice.

ohgodwhat
Aug 6, 2005

So I'm having massive troubles with conda. We're building packages of our own libraries (that do have compiled components) using conda build, and then installing those packages into new environments as a means to deploy them. Half the time this works fine, the other half of the time python doesn't get linked into the environment at all. This is with the same exact packages and the same process to deploy it. There will be envname/bin/python pointing at envname/bin/python3 pointing at envname/bin/python33. but python33 isn't there, even though conda says it's installed/linked it. Of course, conda doesn't throw an error when it symlinks files that don't exist. In fact conda seems to think everything is just fine. Running 'conda install python' will say it's installed python 3.3.5, but still no python will be accessible, however 'conda install python=3.3.2' will actually install/link python, but then none of the installed packages will be found.

How can you be on major version 3 of some software and still completely break in simple scenarios without even telling the user?

I know this sounds ungrateful, and I know things would be worse without conda, but it's really hard for me to appreciate that when I can't even consistently achieve results using its basic functionality.

Space Kablooey
May 6, 2009



How is dogs.append(first) not throwing a NameError in your print method?

By the way, in addition to the __str__ magic method, you can do someting like this:

Python code:
class Dog:
    # (...)
   
    @staticmethod 
    def print_dogs(list_of_dogs): 
        for dog in list_of_dogs:
            print dog
	
dogs = []                                 #empty dog list in which to store objects
if __name__ == "__main__":
    while True:
        name = input("Name: ")
        breed = input("Breed: ")
        dogs.append(Dog(name, breed))  # No need to store a new instance of Dog into a variable, just add it to the list 
        Dog.print_dogs(dogs)  # (*)
(*): Since print_dogs is a static method (the @staticmethod bit), you don't need a instance of a Dog to execute the method. You just pass in the list of all dogs and let the method handle it however.

I suggest you also look into a bit called @classmethod, and see how it differs from @staticmethods and regular methods.
Anything not clear, please ask!

Space Kablooey fucked around with this message at 04:46 on Jun 11, 2014

hlfrk414
Dec 31, 2008
It's not throwing an exception because first is in the global namespace. if __name__ does not create a main method, and just because first is define at the bottom of the script doesn't stop the Dog class from getting it a runtime. Remember, the beginning of the file isn't the global namespace, the entire file is.

I would not suggest giving a new python programmer decorator methods either; just define a function outside the class. It's the simplest to understand and a good habit to build first. Functions that don't act on the class object or the instance can be plain functions. Not harpin' on you for helping though.

BigRedDot
Mar 6, 2008

ohgodwhat posted:

So I'm having massive troubles with conda. We're building packages of our own libraries (that do have compiled components) using conda build, and then installing those packages into new environments as a means to deploy them. Half the time this works fine, the other half of the time python doesn't get linked into the environment at all. This is with the same exact packages and the same process to deploy it. There will be envname/bin/python pointing at envname/bin/python3 pointing at envname/bin/python33. but python33 isn't there, even though conda says it's installed/linked it. Of course, conda doesn't throw an error when it symlinks files that don't exist. In fact conda seems to think everything is just fine. Running 'conda install python' will say it's installed python 3.3.5, but still no python will be accessible, however 'conda install python=3.3.2' will actually install/link python, but then none of the installed packages will be found.

How can you be on major version 3 of some software and still completely break in simple scenarios without even telling the user?

I know this sounds ungrateful, and I know things would be worse without conda, but it's really hard for me to appreciate that when I can't even consistently achieve results using its basic functionality.

So this sounds really weird. Whether it's a bug or just some docs/communication issue, it should get resolved ASAP. If you will post details on the conda mailing list I will make sure Aaron or Ilan sees it.

Dominoes
Sep 20, 2007

I'd use HardDisk's solution: __str__() and a list of objects.

Malcolm XML
Aug 8, 2009

I always knew it would end like this.

Suspicious Dish posted:

WinRT doesn't really do anything other than add another set of low-level APIs to Windows that MS will deprecate and forget about in two years. There isn't any attempt at solving high-level problems, only the latest replacement for OLE/COM/COM+/DCOM/ATL/MFC/.NET/WPF/Silverlight/XAML.

Going ot here, but Winrt is built on com .net xaml and the rest of the tech and is even native to boot. If you're building GUIs for windows it's the nicest place to be. No backwards compat to worry about.

It's worth looking into seeing if ironpython works on it.

For what it's worth though, the best Python GUI kit is serving an HTML ui and wrapping it in a native web control. Python GUI bindings are usually junk and are more trouble than it's worth.

Space Kablooey
May 6, 2009


hlfrk414 posted:

I would not suggest giving a new python programmer decorator methods either; just define a function outside the class. It's the simplest to understand and a good habit to build first. Functions that don't act on the class object or the instance can be plain functions. Not harpin' on you for helping though.

Fair enough. I don't know decorators methods myself as well as I should, and I really should brush up on that.

What I wanted to say with that, is that he should look up static classes/methods, but in the general OOP context. That it was implemented via a decorator method just muddled things up.

ohgodwhat
Aug 6, 2005

BigRedDot posted:

So this sounds really weird. Whether it's a bug or just some docs/communication issue, it should get resolved ASAP. If you will post details on the conda mailing list I will make sure Aaron or Ilan sees it.

Thanks, I appreciate that a lot. A senior dev here has been banging his head against a wall with this for a couple days now, and I've had no luck either. Hopefully it's just something we're doing wrong that's very trivial, but our inability to reproduce it has made it tough to test. I've thought I've fixed it several times now, only to find out that something that was working has broken.

I wouldn't be so frustrated if conda wasn't worth it, I'd just do something else.

BigRedDot
Mar 6, 2008

ohgodwhat posted:

Thanks, I appreciate that a lot. A senior dev here has been banging his head against a wall with this for a couple days now, and I've had no luck either. Hopefully it's just something we're doing wrong that's very trivial, but our inability to reproduce it has made it tough to test. I've thought I've fixed it several times now, only to find out that something that was working has broken.

I wouldn't be so frustrated if conda wasn't worth it, I'd just do something else.

Well we definitely appreciate the help to improve it and make it better (no better way to sharpen tools than to bang them on other people's problems). I sent an internal email about this but definitely write up something for the mailing list if you can, because they may want to ask for more details. Alternatively, the GH issue tracker is a good place too: https://github.com/conda/conda-build

BigRedDot
Mar 6, 2008

Oh BTW the videos from PyData Silicon Valley 2014 are up!

https://www.youtube.com/user/PyDataTV

Lots of great talks, covering Python with Storm, Kafka, SciDB, machine learning tools, IPython widgets, Conda, Wes Mckinney (of Pandas fame) unveiling DataPad, Rob Story's Python visualization "mega talk", and many others. My extended (three hour!) Bokeh Tutorial is up as well. If you use python and, uh, have data, you'll probably find something useful.

Tacos Al Pastor
Jun 20, 2003

HardDisk posted:

How is dogs.append(first) not throwing a NameError in your print method?

By the way, in addition to the __str__ magic method, you can do someting like this:

Python code:
class Dog:
    # (...)
   
    @staticmethod 
    def print_dogs(list_of_dogs): 
        for dog in list_of_dogs:
            print dog
	
dogs = []                                 #empty dog list in which to store objects
if __name__ == "__main__":
    while True:
        name = input("Name: ")
        breed = input("Breed: ")
        dogs.append(Dog(name, breed))  # No need to store a new instance of Dog into a variable, just add it to the list 
        Dog.print_dogs(dogs)  # (*)
(*): Since print_dogs is a static method (the @staticmethod bit), you don't need a instance of a Dog to execute the method. You just pass in the list of all dogs and let the method handle it however.

I suggest you also look into a bit called @classmethod, and see how it differs from @staticmethods and regular methods.
Anything not clear, please ask!

Actually that wont work. Trying to print objects that are stored in a list produces the following output:

Name: spot
Breed: wolf
<__main__.Dog object at 0x59ae70>

edit:

Got it!

Python code:
dogs = []                                 #empty dog list in which to store objects

class Dog:
    def __init__(self = None, name = None, breed = None):
        self.name = name
        self.breed = breed
    
    def __str__(self):
        return '{}: {}'.format(self.name, self.breed)       
        
if __name__ == "__main__":
    while True:
        name = input("Name: ")
        breed = input("Breed: ")
        first = Dog(name, breed)
        dogs.append(first)
        print("Dogs")        
        for items in dogs:
            print(items)
__str__() method helped!

Thanks guys :)

Tacos Al Pastor fucked around with this message at 01:36 on Jun 13, 2014

Symbolic Butt
Mar 22, 2009

(_!_)
Buglord

spiralbrain posted:

Python code:
dogs = []                                 #empty dog list in which to store objects

class Dog:
    def __init__(self = None, name = None, breed = None):
        self.name = name
        self.breed = breed
    
    def __str__(self):
        return '{}: {}'.format(self.name, self.breed)       
        
if __name__ == "__main__":
    while True:
        name = input("Name: ")
        breed = input("Breed: ")
        first = Dog(name, breed)
        dogs.append(first)
        print("Dogs")        
        for items in dogs:
            print(items)

Why are you setting self from __init__ as an optional parameter?

FoiledAgain
May 6, 2007

Symbolic Butt posted:

Why are you setting self from __init__ as an optional parameter?

Why does that even work in the first place? How come it doesn't raise something like AttributeError: 'NoneType' object has no attribute 'name'?

KICK BAMA KICK
Mar 2, 2009

FoiledAgain posted:

Why does that even work in the first place? How come it doesn't raise something like AttributeError: 'NoneType' object has no attribute 'name'?
The interpreter always provides the object itself as the first argument to the method -- no matter how you invoke the method it can't be omitted as long as the method isn't defined as static, so the default value of None is never used.

the
Jul 18, 2004

by Cowcaster
I've been experimenting sending queries to Salesforce with a module called Beatbox. Entries are returned as type "instance," and I'd like to save them to a list.

I have something like this:

Python code:
for rec in qr1[sf.records:]:
     print str(rec)
Which would print the entire record without spacing, like "JohnSmith123SycamoreStreet"

If I instead do:

Python code:
for rec in qr1[sf.records:]:
     for i in range(0,len(rec),1):
           print str(rec[i])
I get:

code:
John Smith
123 Sycamore Street
My problem I'm having is that I want to store this information in a list. The problem comes when I try to append the information, if I have something like this:

code:
for rec in qr1[sf.records:]:
	for i in range(0,len(rec),1):
		list1.append(str(rec[i]))
It ends up writing each entry as a separate list entry, which is not what I want. 100 entries with 5 items each ends up writing 500 entries to the list. I'd like to get something like:

code:
list1 = ['John Smith, 123 Sycamore Street','Jane Doe, 105 1st Avenue']

SurgicalOntologist
Jun 17, 2004

First of all, instead of

Python code:
for i in range(0,len(rec),1):
    entry = rec[i]  # Or whatever you're doing with it.
do

Python code:
for entry in rec:
Second of all, don't use variable names that end in numbers. If you plan to have more than one of something, use a list or other data structure.

Thirdly, I think you're looking for the str method join:

Python code:
all_records = []
for rec in qr1[sf.records:]:
    all_records.append(', '.join(rec))
or

Python code:
all_records = [', '.join(rec) for rec in qr1[sf.records:]]

Haystack
Jan 23, 2005





fake edit: what SurgicalOntologist said

Just as a general comment, you should really always use descriptive variable names. You might know what sf means now, but I sure as hell don't, and you likely won't a year later. It's worth doing even in one-off scripts since it helps you reason about your own code (not to mention that one-offs have a nasty habit of sneaking into larger projects).

Adbot
ADBOT LOVES YOU

QuarkJets
Sep 8, 2008

List comprehensions are my favorite Python feature

e: And as an addition to the poster above me, short variable names can make it impossible to modify your code later. If you have to search for a variable called 'd' then you're pretty much hosed if you have any variables with a d in their name

  • Locked thread