How to JSON serialize sets?

I have a Python set that contains objects with __hash__ and __eq__ methods in order to make certain no duplicates are included in the collection. I need to json encode this result set, but passing even an empty set to the json.dumps method raises a TypeError. File “/usr/lib/python2.7/json/encoder.py”, line 201, in encode chunks = self.iterencode(o, … Read more

How to get the name of an exception that was caught in Python?

How can I get the name of an exception that was raised in Python? e.g., try: foo = bar except Exception as exception: name_of_exception = ??? assert name_of_exception == ‘NameError’ print “Failed with exception [%s]” % name_of_exception For example, I am catching multiple (or all) exceptions, and want to print the name of the exception … Read more

How to activate an Anaconda environment

I’m on Windows 8, using Anaconda 1.7.5 64bit. I created a new Anaconda environment with conda create -p ./test python=2.7 pip from C:\Pr\TEMP\venv\. This worked well (there is a folder with a new python distribution). conda tells me to type activate C:\PR\TEMP\venv\test to activate the environment, however this returns: No environment named “C:\PR\temp\venv\test” exists in … Read more

Check if a value exists in pandas dataframe index

I am sure there is an obvious way to do this but cant think of anything slick right now. Basically instead of raising exception I would like to get True or False to see if a value exists in pandas df index. import pandas as pd df = pd.DataFrame({‘test’:[1,2,3,4]}, index=[‘a’,’b’,’c’,’d’]) df.loc[‘g’] # (should give False) … Read more

pythonw.exe or python.exe?

Long story short: pythonw.exe does nothing, python.exe accepts nothing (which one should I use?) test.py: print “a” CMD window: C:\path>pythonw.exe test.py <BLANK LINE> C:\path> C:\path>python.exe test.py File “C:\path\test.py”, line 7 print “a” ^ SyntaxError: invalid syntax C:\path> Please tell me what I’m doing terrible wrong. 6 Answers 6