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
Jewel
May 2, 2009

hooah posted:

Alright, forgoing the hotkey thing for a bit, why doesn't this work?
Python code:
def nested_sum(list):
    total = 0
    for x in list:
        if type(x) == 'list':  # Not too familiar with 'type', tried just list also.
            total += sum(x)
        else:
            total += x
    return total

print nested_sum([1, 2, [1, 2]])
I get a runtime error when x = [1, 2] because the if fails. Why doesn't type(x) return list (or 'list')?

Because it IS returning list.



Also the better way to do this (though duck typing is bad in general) is to use "if isinstance(x, list)"

Edit: vvv :aaaaa: I completely missed that, that's absolutely right.

Jewel fucked around with this message at 01:42 on Oct 19, 2014

Adbot
ADBOT LOVES YOU

KICK BAMA KICK
Mar 2, 2009

You had the right idea without quotes around 'list' in the condition -- list, a reference to the built-in type, is what type should return when given [1, 2] -- not 'list', a string bearing its name. There's one problem: you used "list" as the name of your function's argument. In the scope of your function, list now refers to the value [1, 2, [1, 2]] and not the built-in type it refers to everywhere else. So you're testing if type([1, 2]) returns [1, 2, [1, 2]], which it of course does not.

So that's why you never overwrite the name of a built-in. Use pretty much anything else as the name of your function's argument, remove those quotes and it will work fine. An IDE like PyCharm will warn you when you do something like that.

(Also what the above said about using isinstance, and, even better, testing against I think collections.Iterable to catch tuples, sets and the like.)

KICK BAMA KICK fucked around with this message at 01:40 on Oct 19, 2014

hooah
Feb 6, 2006
WTF?
gently caress, I really should know better than to shadow built-in stuff after over a year of C++. Also, didn't know about isinstance yet, thanks. Back to Think Python!

Symbolic Butt
Mar 22, 2009

(_!_)
Buglord

KICK BAMA KICK posted:

(Also what the above said about using isinstance, and, even better, testing against I think collections.Iterable to catch tuples, sets and the like.)

Just a note: It's collections.abc.Iterable now. https://docs.python.org/3/library/collections.abc.html

tef
May 30, 2004

-> some l-system crap ->

KICK BAMA KICK posted:

(Also what the above said about using isinstance, and, even better, testing against I think collections.Iterable to catch tuples, sets and the like.)

>>> isinstance("a",collections.Iterable)
True

That test also returns true for sequences, like strings. On the other hand hasattr(x, "__iter__"), will pick up any iterable, but not strings.

KICK BAMA KICK
Mar 2, 2009

tef posted:

>>> isinstance("a",collections.Iterable)
True

That test also returns true for sequences, like strings. On the other hand hasattr(x, "__iter__"), will pick up any iterable, but not strings.
Fair point for Python 2 but strings do have an __iter__ method in Python 3.

I assume in the real world the correct way to solve the original problem would involve something clever with itertools.chain.

tef
May 30, 2004

-> some l-system crap ->

KICK BAMA KICK posted:

Fair point for Python 2 but strings do have an __iter__ method in Python 3.

:argh:

Suspicious Dish
Sep 24, 2011

2020 is the year of linux on the desktop, bro
Fun Shoe
I assume nested_sum is for a class, but for future reference, you shouldn't ever have weird data structures like that.

ShadowHawk
Jun 25, 2000

CERTIFIED PRE OWNED TESLA OWNER
Has anyone here legit used bitwise operators in Python in their career yet? (Other than job interview questions -- I got grilled on it as a first question and was a bit puzzled as to why)

suffix
Jul 27, 2013

Wheeee!

ShadowHawk posted:

Has anyone here legit used bitwise operators in Python in their career yet? (Other than job interview questions -- I got grilled on it as a first question and was a bit puzzled as to why)

Sure, I've used them to compute hamming distances in a search application prototype, for doing time-constant string compares (though normally you should be using hmac.compare_digest() or a similar library function), and I'm using them as I'm working my way through the Matasano crypto challenges.

They just show up in programming from time to time irrespective of language.

suffix fucked around with this message at 23:37 on Oct 19, 2014

Chosen
Jul 11, 2002
Vibrates when provoked.
I used them when computing numpy arrays as masks for the 2011 ants AI challenge.

BeefofAges
Jun 5, 2004

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

I haven't done a single bitwise operation since the last embedded systems project I worked on in college.

Lysidas
Jul 26, 2002

John Diefenbaker is a madman who thinks he's John Diefenbaker.
Pillbug
I regularly use unary not (~) for negating boolean Pandas data structures (both Series and DataFrames).

Thermopyle
Jul 1, 2003

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

I guess a better question, is has anyone used bitwise operations where they were legitimately better choice over alternative options?

I'll come across them in someone else's code every once in awhile and I've yet to see a case where they weren't just trying to be clever.

I'm sure there are good uses, but from my experience the ratio between times bitwise operations are used and times when bitwise operations should be used is not that great.

Suspicious Dish
Sep 24, 2011

2020 is the year of linux on the desktop, bro
Fun Shoe
I use them to extract color values from RGBA snippets. Or, in more abstract terms, extract records from bit packed formats.

I use them when doing bit-level reading for things like Huffman Coding.

I use them to compute powers of two or upper limits of a specific size. Doing 1 << 8 instead of 2**8 is arguably a matter of taste, though I prefer the former.

Really, bit level twiddling is very useful, and I use the bit twiddling operators whenever I need to bit twiddle.

Murodese
Mar 6, 2007

Think you've got what it takes?
We're looking for fine Men & Women to help Protect the Australian Way of Life.

Become part of the Legend. Defence Jobs.

ShadowHawk posted:

Has anyone here legit used bitwise operators in Python in their career yet? (Other than job interview questions -- I got grilled on it as a first question and was a bit puzzled as to why)

They're overloaded in sqlalchemy's ORM to do OR/AND, but otherwise nope.

Megaman
May 8, 2004
I didn't read the thread BUT...
How can I make a dictionary of dictionaries like the following with the keys coming from two other files:

file one contents:
key1
key2
key3

file two contents:
foo
bar
etc.

{somekey
{key1: undef, foo: undef, bar: undef, etc.}
{key2: undef, foo: undef, bar: undef, etc.}
{key3: undef, foo: undef, bar: undef, etc.}
}

Space Kablooey
May 6, 2009


I'm unclear on what you actually want, but maybe this?


Python code:
Python 2.7.6 (default, Mar 22 2014, 22:59:56) 
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> x = {}
>>> x['somekey'] = []
>>> x['somekey'].append({'foo': 1})
>>> x['somekey'].append({'foo': 2})
>>> x['somekey'].append({'foo': 3})
>>> x
{'somekey': [{'foo': 1}, {'foo': 2}, {'foo': 3}]}
>>> 
Or this:

Python code:
>>> y = {}
>>> 
>>> y['key1'] = {'foo': 1}
>>> y['key2'] = {'foo': 2}
>>> y['key3'] = {'foo': 3}
>>> y
{'key3': {'foo': 3}, 'key2': {'foo': 2}, 'key1': {'foo': 1}}
>>> 

Space Kablooey fucked around with this message at 18:43 on Oct 20, 2014

no_funeral
Jul 4, 2004

why
Can somebody repost the Python random seeded number fizz buzz?

ShadowHawk
Jun 25, 2000

CERTIFIED PRE OWNED TESLA OWNER

Sitting Bull posted:

Can somebody repost the Python random seeded number fizz buzz?

Python code:
#!/usr/bin/env python2
import random

for i in range(0, 100):
    if not i % 15:
        random.seed(1178741599)
    print([i+1, "Fizz", "Buzz", "FizzBuzz"][random.randint(0,3)])

regularizer
Mar 5, 2012

I'm trying to install Pandas using pip, but I keep on getting the following error:

code:
Exception:
Traceback (most recent call last):
  File "/Library/Python/2.7/site-packages/pip-1.5.6-py2.7.egg/pip/basecommand.py", line 122, in main
    status = self.run(options, args)
  File "/Library/Python/2.7/site-packages/pip-1.5.6-py2.7.egg/pip/commands/install.py", line 283, in run
    requirement_set.install(install_options, global_options, root=options.root_path)
  File "/Library/Python/2.7/site-packages/pip-1.5.6-py2.7.egg/pip/req.py", line 1435, in install
    requirement.install(install_options, global_options, *args, **kwargs)
  File "/Library/Python/2.7/site-packages/pip-1.5.6-py2.7.egg/pip/req.py", line 671, in install
    self.move_wheel_files(self.source_dir, root=root)
  File "/Library/Python/2.7/site-packages/pip-1.5.6-py2.7.egg/pip/req.py", line 901, in move_wheel_files
    pycompile=self.pycompile,
  File "/Library/Python/2.7/site-packages/pip-1.5.6-py2.7.egg/pip/wheel.py", line 215, in move_wheel_files
    clobber(source, lib_dir, True)
  File "/Library/Python/2.7/site-packages/pip-1.5.6-py2.7.egg/pip/wheel.py", line 205, in clobber
    os.makedirs(destdir)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/os.py", line 157, in makedirs
    mkdir(name, mode)
OSError: [Errno 13] Permission denied: '/Library/Python/2.7/site-packages/pandas'

Storing debug log for failure in /Users/zacpol/Library/Logs/pip.log
This comes after the install seemingly works and the terminal says 'cleaning up...'. What's going wrong? Is there another way to install Pandas?


E: also, the version of Python that came bundled with osx is located under system/library/frameworks/Python.framework, but the 2.x and 3.x versions I've manually installed are located at library/frameworks/Python.framework. Is there an easy way to get all versions of Python in the same location without breaking anything?

regularizer fucked around with this message at 09:09 on Oct 21, 2014

Dominoes
Sep 20, 2007

Pip tends not to work for packages that use non-Python code. I don't know the details of OSX, but on Linux, you'd use a package, and on Windows, you'd use a binary. Anaconda is another option.

Blinkz0rz
May 27, 2001

MY CONTEMPT FOR MY OWN EMPLOYEES IS ONLY MATCHED BY MY LOVE FOR TOM BRADY'S SWEATY MAGA BALLS
Use Homebrew

Also unless you're using Pandas for a specific project and are using virtualenv it might make sense just to use the Anaconda distribution as it's entirely packaged and you don't have to deal with the insanely long compilation process.

Lyon
Apr 17, 2003

ShadowHawk posted:

Python code:
#!/usr/bin/env python2
import random

for i in range(0, 100):
    if not i % 15:
        random.seed(1178741599)
    print([i+1, "Fizz", "Buzz", "FizzBuzz"][random.randint(0,3)])

That is pretty brilliant in a horrible horrible way... I guess if you first wrote a program to iterate through the first 15 values from each seed until you found a match for FizzBuzz... but who even thinks of these things?

Thermopyle
Jul 1, 2003

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

Dominoes posted:

Pip tends not to work for packages that use non-Python code. I don't know the details of OSX, but on Linux, you'd use a package, and on Windows, you'd use a binary. Anaconda is another option.

A big part of the reason I switched my dev environment to Linux was because libraries that require compiling work with pip.

Johnny Five-Jaces
Jan 21, 2009


Hi guys. Hoping to get a bit of guidance, because I am totally stuck.

I'm attempting to use a library (module?) to create a data visualization. This library requires, among other things, that numpy and scipy be installed. When I attempt to use the module via command line, it gives me the following error:

code:
Traceback (most recent call last):
  File "inchlib_clust.py", line 8, in <module>
    from sklearn import preprocessing
  File "C:\Python27\lib\site-packages\sklearn\preprocessing\__init__.py", line 6
, in <module>
    from .data import Binarizer
  File "C:\Python27\lib\site-packages\sklearn\preprocessing\data.py", line 15, i
n <module>
    from ..utils import check_arrays
  File "C:\Python27\lib\site-packages\sklearn\utils\__init__.py", line 10, in <m
odule>
    from .murmurhash import murmurhash3_32
  File "__init__.pxd", line 861, in init sklearn.utils.murmurhash (sklearn\utils
\murmurhash.c:5245)
ValueError: numpy.ufunc has the wrong size, try recompiling
The Internet seems to indicate that error at the bottom comes from numpy having been compiled for a different version of scipy (or, as I think is the case here, scikit-learn). This is being done on Windows, so I used the executables provided to install everything I am working with. I installed the 32-bit version of Python 2.7, and then installed 32 bit versions of each of the tools that were, according to the site I got them from, compiled for Python version 2.7. I am using numpy 1.7.1, which appears to be compatible with scipy 0.12. I got scikit-learn from this site so that I did not have to compile it myself.

I am pretty (very) new to using python and I'm just not even sure where to look to figure out what is causing this problem. If this isn't the right place to ask or if I should be directed to some reference materials first, please let me know :-)

accipter
Sep 12, 2003

AgentSythe posted:

Hi guys. Hoping to get a bit of guidance, because I am totally stuck.

I'm attempting to use a library (module?) to create a data visualization. This library requires, among other things, that numpy and scipy be installed. When I attempt to use the module via command line, it gives me the following error:

code:
Traceback (most recent call last):
  File "inchlib_clust.py", line 8, in <module>
    from sklearn import preprocessing
  File "C:\Python27\lib\site-packages\sklearn\preprocessing\__init__.py", line 6
, in <module>
    from .data import Binarizer
  File "C:\Python27\lib\site-packages\sklearn\preprocessing\data.py", line 15, i
n <module>
    from ..utils import check_arrays
  File "C:\Python27\lib\site-packages\sklearn\utils\__init__.py", line 10, in <m
odule>
    from .murmurhash import murmurhash3_32
  File "__init__.pxd", line 861, in init sklearn.utils.murmurhash (sklearn\utils
\murmurhash.c:5245)
ValueError: numpy.ufunc has the wrong size, try recompiling
The Internet seems to indicate that error at the bottom comes from numpy having been compiled for a different version of scipy (or, as I think is the case here, scikit-learn). This is being done on Windows, so I used the executables provided to install everything I am working with. I installed the 32-bit version of Python 2.7, and then installed 32 bit versions of each of the tools that were, according to the site I got them from, compiled for Python version 2.7. I am using numpy 1.7.1, which appears to be compatible with scipy 0.12. I got scikit-learn from this site so that I did not have to compile it myself.

I am pretty (very) new to using python and I'm just not even sure where to look to figure out what is causing this problem. If this isn't the right place to ask or if I should be directed to some reference materials first, please let me know :-)

On a phone so this will be short. Don't compile things it just results in headaches. On Windows, I strongly recommend miniconda. After the install just type "conda install scikits-learn" and all dependencies will be installed. The other option is installing all programs from the website you linked. That was my previous method before miniconda.

QuarkJets
Sep 8, 2008

Personally, I like just plain old Anaconda. In either case, installing individuals Python packages in Windows causes headaches, use something like Anaconda or miniconda

Johnny Five-Jaces
Jan 21, 2009


Thanks for the quick response. I had no idea that was even a possibility or something I should be looking at. I will tear down what I attempted today and use that method.

Dominoes
Sep 20, 2007

And like accipter said, Chris Gohlke's lfd.uci site is great for Windows binaries if you're not using Anaconda. It's kept up-to-date, and you just run the installers and the packages work. However, for the more complex packages, you may need to download a separate installer for each dependency.

Dominoes fucked around with this message at 01:50 on Oct 22, 2014

EAT THE EGGS RICOLA
May 29, 2008

ShadowHawk posted:

Python code:
#!/usr/bin/env python2
import random

for i in range(0, 100):
    if not i % 15:
        random.seed(1178741599)
    print([i+1, "Fizz", "Buzz", "FizzBuzz"][random.randint(0,3)])

This is totally cheating unless you find a seed that actually works up to 100.

good jovi
Dec 11, 2000

'm pro-dickgirl, and I VOTE!

EAT THE EGGS RICOLA posted:

This is totally cheating unless you find a seed that actually works up to 100.

It's cheating no matter what, but it does go up to 100 (see the `i+1`). It goes up to any n, actually, since you're reseeding every 15 steps. I suppose it would be possible to find a seed that gave the entire 100-item sequence, but finding this one took long enough.

EAT THE EGGS RICOLA
May 29, 2008

good jovi posted:

It's cheating no matter what, but it does go up to 100 (see the `i+1`). It goes up to any n, actually, since you're reseeding every 15 steps. I suppose it would be possible to find a seed that gave the entire 100-item sequence, but finding this one took long enough.

I meant that it's cheating if your seed doesn't give the entire sequence.

ShadowHawk
Jun 25, 2000

CERTIFIED PRE OWNED TESLA OWNER

EAT THE EGGS RICOLA posted:

This is totally cheating unless you find a seed that actually works up to 100.

To be clear, finding a brute force solution that goes up to 100 is 2^170 times harder.

EAT THE EGGS RICOLA
May 29, 2008

ShadowHawk posted:

To be clear, finding a brute force solution that goes up to 100 is 2^170 times harder.

Yes, but if you're going to do something ridiculous you might as well do it properly.

:haw:

good jovi
Dec 11, 2000

'm pro-dickgirl, and I VOTE!

EAT THE EGGS RICOLA posted:

I meant that it's cheating if your seed doesn't give the entire sequence.

aw jeez man, I had enough to worry about. I guess my first question is whether or not I can expect to find an answer before the heat death of the universe.

Jose Cuervo
Aug 25, 2004
Pandas question. I have the following code and I am having trouble understanding why the warning is being thrown:

Python code:
>>> df = pandas.DataFrame({'a': [1,2,3,4,5,6,7], 'b': [11,22,33,44,55,66,77]})
>>> reduced_df = df[df['a'] > 3]
>>> reduced_df
   a   b
3  4  44
4  5  55
5  6  66
6  7  77
>>> reduced_df['a'] /= 3

Warning (from warnings module):
  File "__main__", line 1
SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_index,col_indexer] = value instead
>>> reduced_df
          a   b
3  1.333333  44
4  1.666667  55
5  2.000000  66
6  2.333333  77
I have read through the documentation but I still don't understand why the warning is being thrown.

supercrooky
Sep 12, 2006

Jose Cuervo posted:

Pandas question. I have the following code and I am having trouble understanding why the warning is being thrown:

Python code:
>>> df = pandas.DataFrame({'a': [1,2,3,4,5,6,7], 'b': [11,22,33,44,55,66,77]})
>>> reduced_df = df[df['a'] > 3]
>>> reduced_df
   a   b
3  4  44
4  5  55
5  6  66
6  7  77
>>> reduced_df['a'] /= 3

Warning (from warnings module):
  File "__main__", line 1
SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_index,col_indexer] = value instead
>>> reduced_df
          a   b
3  1.333333  44
4  1.666667  55
5  2.000000  66
6  2.333333  77
I have read through the documentation but I still don't understand why the warning is being thrown.

By doing your selection in two parts you've wound up with a copy of your original data and assigned to that. If you look at your original dataframe, you'll see it is unchanged.

If instead you do
code:
df = pd.DataFrame({'a': [1,2,3,4,5,6,7], 'b': [11,22,33,44,55,66,77]})
df.loc[df['a'] > 3, 'a'] / = 3
it will modify the original data frame:

code:
In[9]: df
Out[8]: 

          a   b
0  1.000000  11
1  2.000000  22
2  3.000000  33
3  1.333333  44
4  1.666667  55
5  2.000000  66
6  2.333333  77

hooah
Feb 6, 2006
WTF?
Every time I start PyCharm, I get a notification from Soluto that Python has crashed. I also have noticed that I need to set the interpreter every time I start. Any ideas on either of these problems?

Adbot
ADBOT LOVES YOU

Ahz
Jun 17, 2001
PUT MY CART BACK? I'M BETTER THAN THAT AND YOU! WHERE IS MY BUTLER?!

hooah posted:

Every time I start PyCharm, I get a notification from Soluto that Python has crashed. I also have noticed that I need to set the interpreter every time I start. Any ideas on either of these problems?

I had a bunch of problems with Pycharm crashing when I realized Linux was maxing out my VM drive storage with garbage logs in xsession-errors.

  • Locked thread