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
Rohaq
Aug 11, 2006
Sublime Text 2 looks pretty awesome, but it seems pretty expensive for a text editor.

Adbot
ADBOT LOVES YOU

Thermopyle
Jul 1, 2003

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

If you like the idea of Eclipse+PyDev, give that up and get PyCharm instead. It's not free, but it's pretty frickin' great.

ST2 is also awesome, and is what I usually use when working on simple projects.

Dren
Jan 5, 2001

Pillbug

deimos posted:

This is multi-selection editing:



That looks like the column editing mode (cua-mode) in emacs. (Which I use all the time)

Rohaq
Aug 11, 2006

Dren posted:

That looks like the column editing mode (cua-mode) in emacs. (Which I use all the time)
Notepad++ can also do the same, it can be pretty useful.

deimos
Nov 30, 2006

Forget it man this bat is whack, it's got poobrain!

Dren posted:

That looks like the column editing mode (cua-mode) in emacs. (Which I use all the time)

The way it's used is like column mode, but understand multi-selection are entirely arbitrary cursor positions, does NOT have to be the same column everywhere, the example on https://www.sublimetext.com makes it clearer.

Nybble
Jun 28, 2008

praise chuck, raise heck

deimos posted:

This is multi-selection editing:


Smart rearrange is also awesome:


I knew about Smart Rearrange, but that multi-selection editing :aaaaa:

Suspicious Dish
Sep 24, 2011

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

Dren posted:

That looks like the column editing mode (cua-mode) in emacs. (Which I use all the time)

Somebody implemented it in emacs, of course. It's a bit buggy, though.

Jewel
May 2, 2009

deimos posted:

This is multi-selection editing:


Someone pointed out to me that multi-selection editing is in almost every text editor other than notepad.

Just hold alt and highlight stuff in notepad++ (and I think it's just alt in Visual Studio too) and there you go, multi-selection editing. I use it all the time.

Dominoes
Sep 20, 2007

Solution to a question I posted a month ago:

Dominoes posted:

Another CSV / memory vs drive write question. If I want to download a CSV from a url, is there a way I can put the file information directly into a variable in memory? I've tried many combinations using list(), read(), csv.reader() etc, but can't find a working solution that skips writing the csv to disk, and reading it.

Code like this works:
Python code:
    def function():
        u = urlopen('http://somethingawful.com/COBOL.csv')
        with open('drive_file.csv', 'wb') as f:
            f.write(u.read())
        
        with open('drive_file.csv', 'r') as f:
            return list(csv.reader(f))
For example, this seems like it should do what I'm asking, but doesn't.

Python code:
    def function():
        u = urlopen('http://somethingawful.com/COBOL.csv')
        return list(csv.reader(u.read()))

Got it working by 1: Using Requests instead of urllib.request, and 2: using splitlines().

Here's what the working code looks like:
Python code:
def function():
	u = requests.get('http://somethingawful.com/COBOL.csv').text
        return list(csv.reader(u.splitlines()))
It appears that the trouble I was having was due to urllib.request's inability to pass text data instead of binary. Saving the file as an intermediary worked because you can explicitly open local files as 'r' instead of 'rb'.

Dominoes fucked around with this message at 22:15 on May 4, 2013

Thermopyle
Jul 1, 2003

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

I like requests because it just works and I don't have to think about dealing with setting up urllib correctly. It's pretty nice.

BeefofAges
Jun 5, 2004

Cry 'Havoc!', and let slip the cows of war.

requests is so good that it should be part of the standard library.

Winkle-Daddy
Mar 10, 2007
Quick python question. I've been tinkering with a game in Panda3D but hit a performance wall (I should have known better than use a 3D engine for a 2D game), but what's done is done and I've found the only critical thing I was using panda3D for was the perlin noise generation. Usage was basically:

code:
myWorld = PerlinNoise2(100,100)
print(myWorld(2,2))
This will print a value between 0 and 1, where 1 is the brightest point and 0 is the darkest point. I've been searching for a pure python implementation to do this one task as I'd hate to eventually be building against two different game engines. Anyone know of anything pre-made that I can use essentially the same way? Math is not my strong suit.

e:
This also allowed for 3 and 4D perlin noise so that I can do further generation; such as time based or layer based.

e2:
I just installed the python noise plug-in. I tried the example 2dtexture.py, but am not sure how the hell to re-use the pgm format it saves the output to. Ideally, I'd like to use it the exact same way I used the panda3D implementation. Anyone able to help clarify my stupidity with a small example?

Winkle-Daddy fucked around with this message at 19:09 on May 6, 2013

Hammerite
Mar 9, 2007

And you don't remember what I said here, either, but it was pompous and stupid.
Jade Ear Joe
I feel like this is the sort of thing I'm going to slap myself on the forehead for not remembering by myself, but suppose I have a dictionary and I want to transform the values of that dictionary in some way while keeping their keys the same... how do I do that? I mean, obviously I can come up with a way to do that using a loop, but what's the idiomatic way?

What I want to do is make a dictionary whose values are tuples to be passed to a class constructor, and I then want to run the class constructor across the dictionary so as to produce a dictionary of objects of the class.

Python 3 if it matters.

Python code:
mydict = {
    'a': (2, 'cat', 42),
    'b': (3, 'dog', 420),
    'c': (5, 'mouse', 1234),
    'd': (7, 'guinea pig', 5678),
}

DO_SOMETHING(MyClass, mydict)
What I'm looking for is something basically similar to map(), but of course map() returns an iterable "map object", and I want to preserve the keys of my dictionary.

Dren
Jan 5, 2001

Pillbug
You could use a dictionary comprehension:

Python code:
newdict = { k : MyClass(v) for (k, v) in mydict.iteritems() }
# pre-python 2.6 didn't have dict comprehensions so it'd look like this:
newdict = dict((k, MyClass(v)) for (k, v) in mydict.iteritems())
Mind you there's nothing wrong with using a for loop to do this.

Civil Twilight
Apr 2, 2011

How about
Python code:
newdict = dict((k, MyClass(*v)) for (k, v) in mydict.iteritems())
e: f,b

Hammerite
Mar 9, 2007

And you don't remember what I said here, either, but it was pompous and stupid.
Jade Ear Joe
I see, I felt sure there must be a builtin that does this, but I guess I was mistaken.

The following is what I have:

Python code:
def dictArgsMap (myFunction, myDict):
    for item in myDict:
        myDict[item] = myFunction(*myDict[item])
But I suppose I could go the extra mile and allow it to cope with dictionaries, as well.

Python code:
def dictArgsMap (myFunction, myDict):
    for item in myDict:
        if isinstance(myDict[item], dict):
            myDict[item] = myFunction(**myDict[item])
        else:
            myDict[item] = myFunction(*myDict[item])
Does this seem reasonable? I don't like that "isinstance" very much.

edit: less misleading variable names

Python code:
def mutatingStarmap (myFunction, myCollection):
    for item in myCollection:
        if isinstance(myCollection[item], dict):
            myCollection[item] = myFunction(**myCollection[item])
        else:
            myCollection[item] = myFunction(*myCollection[item])

Hammerite fucked around with this message at 04:01 on May 7, 2013

Suspicious Dish
Sep 24, 2011

2020 is the year of linux on the desktop, bro
Fun Shoe
This seems like a very weird problem to have. Where does the dictionary come from?

Dren
Jan 5, 2001

Pillbug
I am very confused by your example code. Could you show sample input and output data that is correct?

The Insect Court
Nov 22, 2012

by FactsAreUseless
If I understand you correctly, you want to initialize class instances where the arguments passed to the constructors are the values in the dictionary? That should be relatively easy:

code:
dict(map(lambda x: (x, myFunc(x)), myDict))
Map pulls apart the key-value pairs of the dictionary and returns a list of tuples, the dict constructor consumes the list of tuples and and creates a new dictionary from them.

Lysidas
Jul 26, 2002

John Diefenbaker is a madman who thinks he's John Diefenbaker.
Pillbug

The Insect Court posted:

If I understand you correctly, you want to initialize class instances where the arguments passed to the constructors are the values in the dictionary? That should be relatively easy:

code:
dict(map(lambda x: (x, myFunc(x)), myDict))
Map pulls apart the key-value pairs of the dictionary and returns a list of tuples, the dict constructor consumes the list of tuples and and creates a new dictionary from them.

This is less readable and idiomatic than the dict comprehension that Dren posted (though of course dict.iteritems no longer exists in Python 3).

Hammerite, if you want to update your dict in place there probably isn't much room for improvement in your functions/loops. A few suggestions though: you may as well directly loop over key, value in my_dict.items() and you should check for isinstance(value, collections.abc.Mapping) instead of dict.

If you want a new dict with instances instead of parameters, then Dren's dict comprehension is great.

Hammerite
Mar 9, 2007

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

Lysidas posted:

Hammerite, if you want to update your dict in place there probably isn't much room for improvement in your functions/loops. A few suggestions though: you may as well directly loop over key, value in my_dict.items() and you should check for isinstance(value, collections.abc.Mapping) instead of dict.

I concluded that I had no need to update the dict in place after all. So I ended up with more or less what Dren suggested in the first place. I also decided I don't really need that code to cope with dictionaries right now, so it's out.

Python code:
def dictStarmap (myFunction, myCollection):
    return {k: myFunction(*v) for (k, v) in myCollection.items()}
But if I decided I did want to handle dictionaries as well, I could do:

Python code:
from collections.abc import Mapping as MappingABC

def dictStarmap (myFunction, myCollection):
    return {
        k: myFunction(**v) if isinstance(v, MappingABC) else myFunction(*v)
        for (k, v) in myCollection.items()
    }
I'm confused about this collections.abc business though. This module has a dot in the name?? Python 3.2 seemed to find this very confusing, but I installed 3.3 and it loves it.

tef
May 30, 2004

-> some l-system crap ->

Hammerite posted:

I'm confused about this collections.abc business though. This module has a dot in the name?? Python 3.2 seemed to find this very confusing, but I installed 3.3 and it loves it.

"This filename has a / in the name". Collections is a module. abc is a module that lives inside collections.

Winkle-Daddy
Mar 10, 2007

Winkle-Daddy posted:

e2:
I just installed the python noise plug-in. I tried the example 2dtexture.py, but am not sure how the hell to re-use the pgm format it saves the output to. Ideally, I'd like to use it the exact same way I used the panda3D implementation. Anyone able to help clarify my stupidity with a small example?

I'm still trying to use this drat noise module, but I'm not getting any value out of it except 0. If anyone has used this before, can you tell me what I'm doing wrong?

code:
>>> from noise import pnoise2
>>> world = pnoise2(10,10,1)
>>> print(world)
0.0
>>> from noise import pnoise3
>>> x = pnoise3(10, 10, 10, octaves=1, persistence=0.5, repeatx=1024, repeaty=1024, repeatz=1024, base=0)
>>> print(x)
0.0
>>> x = pnoise3(100, 100, 150, octaves=1, persistence=0.5, repeatx=1024, repeaty=1024, repeatz=1024, base=0)
>>> print(x)
0.0
>>> x = pnoise3(100, 100, 150, octaves=1, persistence=0.5, repeatx=1024, repeaty=1024, repeatz=1024, base=125834)
>>> print(x)
0.0
>>> y = pnoise2(15, 10, octaves=1, persistence=0.5, repeatx=100, repeaty=100, base=125834)
>>> print(y)
0.0
>>> y = int(pnoise2(15, 10, octaves=1, persistence=0.5, repeatx=100, repeaty=100, base=1))
>>> print(y)
0
>>> y = int(pnoise2(15, 10, octaves=1, persistence=1, repeatx=100, repeaty=100, base=1))
>>> print(y)
0
>>> y = int(pnoise2(50, 30, octaves=1, persistence=1, repeatx=100, repeaty=100, base=1))
>>> print(y)
0
Nothing I try seems to work as I would expect it to. FWIW here is the help page for pnoise3:

code:
noise3(...)
    noise3(x, y, z, octaves=1, persistence=0.5, repeatx=1024, repeaty=1024, repeatz=1024, base=0.0)

    return perlin "improved" noise value for specified coordinate

    octaves -- specifies the number of passes for generating fBm noise,
    defaults to 1 (simple noise).

    persistence -- specifies the amplitude of each successive octave relative
    to the one below it. Defaults to 0.5 (each higher octave's amplitude
    is halved). Note the amplitude of the first pass is always 1.0.

    repeatx, repeaty, repeatz -- specifies the interval along each axis when
    the noise values repeat. This can be used as the tile size for creating
    tileable textures

    base -- specifies a fixed offset for the input coordinates. Useful for
    generating different noise textures with the same repeat interval
I'm reading "base" to basically be what would commonly be called the "seed" value, though I'm not actually sure that's correct. I'm also not entirely sure what persistence is, but it does lead me to believe I should get return values of at least 1.0 for any given coordinate and not 0.

Please help me with my dumb.

n0manarmy
Mar 18, 2003

Does anyone have any idea on how to get the state of a ttk button?

I have code that button A calls, based on the state of button B, button A will change button B's state and set a variable. However I cannot figure out how to determine button B's state with an if statement.

code:
if buttonB.state() != 'disabled':
    blahblahblah
EDIT:
In frustration with how annoying it was to find the answer, I tried to install PyQt and decided I was just going to use that. Well installing PyQt failed me for some reason so I went back to tkinter and figured it out!

code:
if button1.cget('state') == 'active':
    blahblahblahblah
EDIT2:

Well that works in getting me what I want in terms of return but not the expected results :(

The below function does nothing:

code:
def p1ButtonActivate(self):
    if self.p1Button.cget('state') == 'normal':
        self.p1Button.configure(state='active')
        self.p1JonesButton.config(state='disabled')
    if self.p1JonesButton.cget('state') == 'disabled':
        self.p1Button.config(state='normal')
        self.p1JonesButton.config(state='normal')
Where as if I set the function to do this, it does work, but only once

code:
def p1ButtonActivate(self):
    self.p1Button.configure(state='active')
    self.p1JonesButton.config(state='disabled')

n0manarmy fucked around with this message at 18:58 on May 7, 2013

Winkle-Daddy
Mar 10, 2007
I don't use tkinter, but when I was looking at it previously, I thought it was basically:

code:
if(buttonB.config(state="disabled")):
    blahblahblah
else:
    blahblahblah
I just tried it but apparently my python install got royally hosed on my work computer so cannot confirm at this time.


This way probably doesn't work that well, and your way looks more python-ish.

E2: ^^^ If you're not tied to the idea of using tkinter, have you considered wxPython? I've been using it for a couple of months now and it is by far the easiest GUI dev work I've done, especially once combined with the power of wxGlade (once you get over it's quirkyness.)

Winkle-Daddy fucked around with this message at 19:00 on May 7, 2013

n0manarmy
Mar 18, 2003

Winkle-Daddy posted:



E2: ^^^ If you're not tied to the idea of using tkinter, have you considered wxPython? I've been using it for a couple of months now and it is by far the easiest GUI dev work I've done, especially once combined with the power of wxGlade (once you get over it's quirkyness.)

how is wxPython with python3? I see on their site that they specifically show 2.6 and 2.7 as the versions available with their compiled download?

Winkle-Daddy
Mar 10, 2007

n0manarmy posted:

how is wxPython with python3? I see on their site that they specifically show 2.6 and 2.7 as the versions available with their compiled download?

I'm not sure, I've been tied to a game engine (two of them actually) which both conveniently work perfectly with Python 2.7. However, doing some looking around I did find this. So, the binaries exist for it under 3.0-3.3, how well they work with wxGlade, I'm just not sure. My issue with tkinter is exclusively cosmetic; to me, it just "feels" like Windows 3.1, while QT and wxWidgets both hook into the native OS UI API, so it just "looks right" on whatever system it's targeted for.

e: VVV not sure how helpful this will be as I've not used that before, but I had a very similar issue in Aptana Studio where it would only see some of my modules. It turned out that I had two versions of python installed and I was pointing to the wrong one in the PyDev config. I didn't want to make a new post to reply as I have a feeling you've already ruled this possibility out. But if you're on a unix-like system, I'd start with which python, followed by seeing if there are multiple versions installed (or if which python returns a file path which is symlinked to another version, had problems with that, too!).

Winkle-Daddy fucked around with this message at 20:55 on May 7, 2013

Thermopyle
Jul 1, 2003

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

I've got a problem that isn't really a problem but it's irritating the hell out of me.

ipython and pycharm can no longer "see" some python modules like, for example, datetime.

PyCharm gives me the red squiggly saying it's an unrecognized package:



ipython won't tab-complete datetime when I type datet<TAB>.

Everything works fine when I actually use the import, but for some reason ipython and pycharm can't see that it's a real thingy I can import.

I'm using a virtualenv and if I switch to the system python it works fine. It's just when using the virtualenv that I have a problem.

Like I said, things work, but this is just irritating me. I tried deleting the virtualenv and recreating it, but it still didn't work.

Thern
Aug 12, 2006

Say Hello To My Little Friend
Did you tell PyCharm to use the virtualenv interpreter? It won't see packages installed for the virtualenv if you don't do that.

You have to add it in Project Settings > Project Interpreter > Python Interpreters.

Thermopyle
Jul 1, 2003

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

Thern posted:

Did you tell PyCharm to use the virtualenv interpreter? It won't see packages installed for the virtualenv if you don't do that.

You have to add it in Project Settings > Project Interpreter > Python Interpreters.

Yeah, I did that.

peepsalot
Apr 24, 2007

        PEEP THIS...
           BITCH!

I have a crazy emulator written in python, where i have register data stored as ints. Registers are limited to 32bit values, and depending on the operations performed can be treated as single precision floats. I need to convert the integer value to what it's binary data interpreted as floating point would represent.

So 0x3F800000 would convert to 1.0 etc.
http://www.binaryconvert.com/result_float.html?decimal=049

Is there some way to do this?

peepsalot fucked around with this message at 21:20 on May 7, 2013

Lysidas
Jul 26, 2002

John Diefenbaker is a madman who thinks he's John Diefenbaker.
Pillbug
What kind of virtualenv? One created by the old virtualenv or pyvenv in 3.3? For 3.3 native virtualenvs I've always had to manually add $VIRTUAL_ENV/lib/python3.3/site-packages to the paths for that interpreter.

Lysidas
Jul 26, 2002

John Diefenbaker is a madman who thinks he's John Diefenbaker.
Pillbug

peepsalot posted:

I have a crazy emulator written in python, where i have register data stored as ints. Registers are limited to 32bit values, and depending on the operations performed can be treated as single precision floats. I need to convert the integer value to what it's binary data interpreted as floating point would represent.

So 0x3F800000 would convert to 1.0 etc.
http://www.binaryconvert.com/result_float.html?decimal=049

Is there some way to do this?

Python code:
>>> import struct
>>> one_point_zero = b'\x3f\x80\x00\x00'
>>> struct.unpack('>f', one_point_zero)
(1.0,)
Note the > for "big-endian".

peepsalot
Apr 24, 2007

        PEEP THIS...
           BITCH!

Lysidas posted:

Python code:
>>> import struct
>>> one_point_zero = b'\x3f\x80\x00\x00'
>>> struct.unpack('>f', one_point_zero)
(1.0,)
Note the > for "big-endian".
Ok, but I don't have a string, I have an int. Is there a builtin function to turn my int into a string like that?

Lysidas
Jul 26, 2002

John Diefenbaker is a madman who thinks he's John Diefenbaker.
Pillbug
struct.pack

peepsalot
Apr 24, 2007

        PEEP THIS...
           BITCH!

oh, right. I feel a little silly now. Still seems a bit convoluted but I guess that will work.

Lysidas
Jul 26, 2002

John Diefenbaker is a madman who thinks he's John Diefenbaker.
Pillbug
I wouldn't consider this to be very convoluted. When your task is "take the underlying bytes that make up this integer, and interpret them as a float", I think it's reasonable to have to use an actual byte string as an intermediate value. Python's type system doesn't natively allow this kind of casting.

You can drop the endianness specification since you already have an integer:
Python code:
>>> from struct import pack, unpack
>>> unpack('f', pack('i', 0x3f800000))
(1.0,)

Thermopyle
Jul 1, 2003

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

Lysidas posted:

What kind of virtualenv? One created by the old virtualenv or pyvenv in 3.3? For 3.3 native virtualenvs I've always had to manually add $VIRTUAL_ENV/lib/python3.3/site-packages to the paths for that interpreter.

I'm using 2.7, so the old virtualenv.

Haystack
Jan 23, 2005





When you go into Settings | Project Interpreter > Python Interpreters is your system python's lib folder included in the paths tab?

Adbot
ADBOT LOVES YOU

ShoulderDaemon
Oct 9, 2003
support goon fund
Taco Defender

Lysidas posted:

You can drop the endianness specification since you already have an integer

Note that there exist platforms where the endianness of integers is not the same as the endianness of floats, because the FPU and the ALU were made as separate units by different teams and cobbled together at the last minute or something.

  • Locked thread