How can I get non-escaped utf-8 output from Django tests? - django-testing

Using Django 1.3 with python 2.7, a testcase like:
# coding=utf-8
from __future__ import unicode_literals, print_function, division
from django.test import TestCase
class SetContextWordsTests(TestCase):
def test_utf8_output(self):
msg = "你好"
self.fail(msg)
Gives me the output:
AssertionError: \u4f60\u597d
I'd like to see:
AssertionError: 你好
The terminal supports utf-8, and utf-8 is printed unescaped everywhere else, only escaping the output from tests.
I've tried doing self.fail(msg.encode('utf-8')) but that results in a
UnicodeDecodeError.
I've tried setting DEFAULT_CHARSET and TESTING_CHARSET, but they don't have an effect.
What do I need to change to get this output?

Related

plotting a graph in an xml file

I want to show the graph using the python code below, but I am getting a syntax error as seen on the image. Please help to correct this error. I am using Python 3
import os
import sys
import fnss
import networkx as nx
import fnss
import cvxopt
import numpy as np
import codecs
import random
import matplotlib.pyplot as plt
topo_str = 'topo_peter.xml'
topology = fnss.read_topology(topo_str)
topology_directed = topology.to_directed(topology)
print nx.info(topology_directed)
nx.draw(topology_directed)
plt.show()
and this is the am getting
File "<ipython-input-1-fa7157dd7268>", line 14
print nx.info(topology_directed)
^
SyntaxError: invalid syntax
Based on the information provided:
Python version: 3
Error message: invalid syntax on line 14 -> print nx.info(topology_directed)
It is clearly a simple syntax error for people using Python 3 to execute a Python 2 statement.
print "something" # is valid for Python 2, but not Python 3
For Python 3, use
print("something", "and more", "even more")
So changing print nx.info(topology_directed) to print(nx.info(topology_directed)) solves the problem.

Pygraphviz and fixed node positions

How do I force pygraphviz to maintain fixed positions for my nodes. Assume that you have the following code
from __future__ import absolute_import
from __future__ import unicode_literals
from __future__ import print_function
from __future__ import division
import pygraphviz as pgv
from _operator import pos
A=pgv.AGraph()
A.add_node(1,color='red',pos="0,1")
A.add_node(2,color='blue',pos="1,10")
A.add_node(3,color='yellow'pos="2,2")
A.add_edge(1,2,color='green')
A.add_edge(2,3)
A.add_edge(2,2,"1")
A.add_edge(1,3)
A.graph_attr['epsilon']='0.001'
print(A.string()) # print dot file to standard output
A.layout('dot') # layout with dot
A.draw('foo.pdf') # write to file
How do I force the nodes to show up at predetermined positions (0.1), (1,10 and respective (2,2)
Looking at http://pygraphviz.github.io/documentation/pygraphviz-1.4rc1/reference/agraph.html and the signature for the draw method, it looks as if layout is only needed if pos is not present.
"
If prog is not specified and the graph has positions (see layout()) then no additional graph positioning will be performed."
In your case you could just try without using A.layout(). just A.draw('foo.pdf')

Import numpy throws error: SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \uXXXX escape

I have installed pyzo and miniconda under Windows 10 and installed numpy and matplotlib using conda install. But when I'm trying to run
import numpy as np
import matplotlib.pyplot as plt
I'm getting this error:
Traceback (most recent call last):
File "<tmp 1>", line 3, in <module>
import numpy
File "c:\users\jakub\miniconda3\lib\site-packages\numpy\__init__.py", line 165, in <module>
from numpy.__config__ import show as show_config
File "c:\users\jakub\miniconda3\lib\site-packages\numpy\__config__.py", line 5
lapack_mkl_info={'libraries': ['mkl_lapack95_lp64', 'mkl_core_dll', 'mkl_intel_lp64_dll', 'mkl_intel_thread_dll'], 'define_macros': [('SCIPY_MKL_H', None), ('HAVE_CBLAS', None)], 'include_dirs': ['c:\users\jakub\miniconda3\\Library\\include'], 'library_dirs': ['c:\users\jakub\miniconda3\\Library\\lib']}
^
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \uXXXX escape
I do not have any non-standard character either in my code nor in the directory structure...I have read many posts referring to similar problems with UTF-8 but this is different as it happens during the initial import.
When conda installs packages, it replaces the prefix, to make things relocatable. Unfortunately, it does not intelligently escape backslashes, so on Windows, these unescaped backslashes lead to the error you see.
In recent versions of conda, we use forward slashes in prefix replacement, and this issue goes away. If you can update conda, go do that. If not, numpy has prefixes in the following files:
"Lib/site-packages/numpy/distutils/site.cfg"
"Scripts/f2py.py"
"Lib/site-packages/numpy/config.py"
"Lib/site-packages/numpy/distutils/config.py"
check the latter 3 especially, and replace any non-escaped backslashes ( \ ) with either escaped ones ( \\ ) or forward slashes
So, for people having trouble in
import numpy
using Windows 10 + Anaconda:
I replaced all single '\' to double '\\' in
\Lib\site-packages\numpy\__config__.py
I could import numpy after that.
This is how it worked for me: double \ before and after users word.
For example: \\users\\admin\anaconda3\sample.wav
I am using python 3.6 on Windows 7
Not sure why the post keeps only one \ after and before users word in the path

Do I have to specify import when Python script is being run in Ipython?

I am writing a script that I know I will run in Ipython.
I start Ipython as ipython --pylab.
This imports numpy, matplotlib, etc.
So do I have to specify these import statements again in my script?
I did not, and my script did not run.
Thanks for your help.
--pylab imports numpy both as * and np. I always use np. to minimize confusion.
If I need numpy in my script, I include the usual import numpy as np line. This lets me run the script from the shell. I can also run it with run ... from within IPython. I could also do an import in Ipython or some other script.
I've never tried omitting the import numpy line, and I don't see any need to start. It doesn't save any time or space. Make a habit of importing what you need, and don't assume the environment will do it for you.
Functions that you define and edit from with in IPython don't need their own import statements.
just tried this script:
def foo1(x):
return np.sum(x)
def foo2(x):
return x.sum()
Obviously I can load it with 'run'. And foo2(np.array([1,2,3])) works because the array uses its own method. But foo1 produces a NameError: global name 'np' is not defined.

Solving error "delimiter must be a 1-character string" while writing a dataframe to a csv file

Using this question: Pandas writing dataframe to CSV file as a model, I wrote the following code to make a csv file:
df.to_csv('/Users/Lab/Desktop/filteredwithheading.txt', sep='\s+', header=True)
But it returns the following error:
TypeError: "delimiter" must be an 1-character string
I have looked up the documentation for this here http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.to_csv.html but I can't figure out what I am missing, or what that error means. I also tried using (sep='\s') in the code, but got the same error.
As mentioned in the issue discussion (here), this is not considered as a pandas issue but rather a compatibility issue of python's csv module with python2.x.
The workaround to solve it is to enclose the separator with str(..). For example, here is how you can reproduce the problem, and then solve it:
from __future__ import unicode_literals
import pandas as pd
df = pd.DataFrame([['a', 'A'], ['b', 'B']])
df.to_csv(sep=',')
This will raise the following error:
TypeError ....
----> 1 df.to_csv(sep=',')
TypeError: "delimiter" must be an 1-character string
The following however, will show the expected result
from __future__ import unicode_literals
import pandas as pd
df = pd.DataFrame([['a', 'A'], ['b', 'B']])
df.to_csv(sep=str(','))
Output:
',0,1\n0,a,A\n1,b,B\n'
In your case, you should edit your code as follows:
df.to_csv('/Users/Lab/Desktop/filteredwithheading.txt', sep=str('\s+'), header=True)
Note that the although the solution to this error was using a string charcter instead of regex, pandas also raises this error when using from __future__ import unicode_literals with valid unicode characters. As of 2015-11-16, release 0.16.2, this error is still a known bug in pandas:
"to_csv chokes if not passed sep as a string, even when encoding is set to unicode" #6035
For example, where df is a pandas DataFrame:
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import pandas as pd
df.to_csv(pdb_seq_fp, sep='\t', encoding='utf-8')
TypeError: "delimiter" must be an 1-character string
Using a byte lteral with the specified encoding (default utf-8 with Python 3) -*- coding: utf-8 -*- will resolve this in pandas 0.16.2: (b'\t') —I haven't tested with previous versions or 0.17.0.
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import pandas as pd
df.to_csv(pdb_seq_fp, sep=b'\t', encoding='utf-8')
(Note that with versions 0.13.0 - ???, it was necessary to use pandas.compat import u; but by 0.16.2 the byte literal is the way to go.)