string vs unicode encoding - Struct() argument - python-2.7

I am experiencing a strange problem that returns the same error, regardless of the encoding I use. The code works well, without the encoding part in Python 2.7.8, but it breaks in 2.7.6 which is the version that I use for all my development.
import MIDI_PY2 as md
import glob
import ast
import os
dir = '/Users/user/Desktop/sample midis/'
os.chdir(dir)
file_list = []
for file in glob.glob('*.mid'):
file_list.append((dir + file))
dir = '/Users/user/Desktop/sample midis/'
os.chdir(dir)
file_list returns this:
[u'/Users/user/Desktop/sample midis/M1.mid',
u'/Users/user/Desktop/sample midis/M2.mid',
u'/Users/user/Desktop/sample midis/M3.mid',
u'/Users/user/Desktop/sample midis/M4.mid']
md.concatenate_midis(file_list,'/Users/luissanchez/Desktop/temp/out.mid') returns this error:
-
TypeError Traceback (most recent call last)
<ipython-input-73-2d7eef92f566> in <module>()
----> 1 md.concatenate_midis(file_list_1,'/Users/user/Desktop/temp/out.mid')
/Users/user/Desktop/sample midis/MIDI_PY2.pyc in concatenate_midis(paths, outPath)
/Users/user/Desktop/sample midis/MIDI_PY2.pyc in midi2score(midi)
/Users/user/Desktop/sample midis/MIDI_PY2.pyc in midi2opus(midi)
TypeError: Struct() argument 1 must be string, not unicode
then I modify the code so the first argument is string, not unicode:
file_list_1 = [str(x) for x in file_list]
which returns:
['/Users/user/Desktop/sample midis/M1.mid',
'/Users/user/Desktop/sample midis/M2.mid',
'/Users/user/Desktop/sample midis/M3.mid',
'/Users/user/Desktop/sample midis/M4.mid']
running the function concatenate_midis with this last list (file_list_1) returns exactly the same error: TypeError: Struct() argument 1 must be string, not unicode.
Does anybody knows what's going on here? concatenate_midi works well in python 2.7.8, but can't figure out why it doesn't work in what I use, Enthought Canopy Python 2.7.6 | 64-bit
Thanks

The error
error: TypeError: Struct() argument 1 must be string, not unicode.
is usually caused by the struct.unpack() function which in older versions of python requires string arguments and not unicode. Check that struct.unpack() arguments are strings and not unicodes.

One possible cause is from __future__ .. statement.
>>> type('a')
<type 'str'>
>>> from __future__ import unicode_literals
>>> type('a')
<type 'unicode'>
Check whether your code contains the statement.

Related

Run Matlab script from Python: TypeError: 'float' object is not iterable

Actually I have a problem when calling a Matlab script from Python.
import matlab.engine
import os
import random
import numpy as np
a=[str(random.randint(1,3)) for _ in range(3)]
print(a)
eng=matlab.engine.start_matlab()
eng.cd("/Users/dha/Documents/MATLAB/test-matlab/",nargout=0)
sr, state=eng.test_func()
print(sr)
print(state)
In fact I want to return "sr" which is a float and an array of integer "state", e.g. sr = 34.31 and state = [1,2,5]. The function test_func() work well on Matlab, but when I run this in Python from terminal (python test_matlab_engine.py) I received the following error:
Traceback (most recent call last):
File "test_matlab_engine.py", line 10, in <module>
sr, state=eng.mabuc_drl(a)
TypeError: 'float' object is not iterable
Anyone please give me the solution. Thank you so much in advance.
It seems that the result from MATLAB to Python has been cut off. If you have two parameters, you only get one which is the first parameter from the MATLAB. So, the question is how to get two or more parameters.
In a word, you should write this in your Python file:
re = eng.your_function_name(parameter1, parameter2, nargout=2)
where re contains two parameters which come from MATLAB.
You can find more information in the official documentation: Call MATLAB Functions from Python

Concatenation of 2 strings failing because of incompatible operands error returned by PyCharm

I'm trying to run the following code :
# -*- coding: utf8 -*-
import requests
from bs4 import BeautifulSoup
link = "https://www.emploi-public.ma/ar/index.asp?p="
number_of_jobs = 0
houceima = u"الحسيمة"
print type(houceima)
for i in range(1,3):
page_link = link+str(i)
print page_link
emp_pub = requests.get(page_link)
soup = BeautifulSoup(emp_pub.content,"lxml")
for link in soup.find_all("a"):
if houceima in link :
print link
But I'm getting following error :
Traceback (most recent call last):
File "scrape_houceima", line 9, in <module>
page_link = link+str(i)
TypeError: unsupported operand type(s) for +: 'Tag' and 'str'
I'm using PyCharm. I stated my IDE because the same concatenation page_link = link+str(i) executed well in IDLE.
What could be the problem here ?
You re-used link in your code:
link = "https://www.emploi-public.ma/ar/index.asp?p="
and
for link in soup.find_all("a"):
The latter use replaces the first link reference, so it is no longer a string object but a Tag object.
Don't mask variables like that, rename one or the other. Perhaps the first use could be named base_url?

Casting in Jython 2.5.3

There is a Python function, that runs in CPython 2.5.3 but crashes in Jython 2.5.3 .
It is part of a user defined function in Apache Pig, which uses Jython 2.5.3 so i cannot change it.
The input is a array of singed bytes, but in fact that are unsigned bytes, so i need to cast it.
from StringIO import StringIO
import array
import ctypes
assert isinstance(input, array.array), 'unexpected input parameter'
assert input.typecode == 'b', 'unexpected input type'
buffer = StringIO()
for byte in input:
s_byte = ctypes.c_byte(byte)
s_byte_p = ctypes.pointer(s_byte)
u_byte = ctypes.cast(s_byte_p, ctypes.POINTER(ctypes.c_ubyte)).contents.value
buffer.write(chr(u_byte))
buffer.seek(0)
output = buffer.getvalue()
assert isinstance(output, str)
The error is:
s_byte = ctypes.cast(u_byte_p, ctypes.POINTER(ctypes.c_byte)).contents.value
AttributeError: 'module' object has no attribute 'cast'
I guess the ctypes.cast functions is not implemeted in Jython 2.5.3 . Is there a workaround for that issue?
Thanks,
Steffen
Here is my solution, that is quite ugly but works without additional dependecies.
It uses the bit representation of usinged und signed bytes (https://de.wikipedia.org/wiki/Zweierkomplement).
import array
assert isinstance(input, array.array), 'unexpected input parameter'
assert input.typecode == 'b', 'unexpected input type'
output = array.array('b', [])
for byte in input:
if byte > 127:
byte = byte & 127
byte = -128 + byte
output.append(byte)

Gzip and Encode file in Python 2.7

with gzip.open(sys.argv[5] + ".json.gz", mode="w", encoding="utf-8") as outfile:
It throws:
TypeError: open() got an unexpected keyword argument 'encoding'
But the docs says it exists
https://docs.python.org/3/library/gzip.html
Update
How can i encode and zip the file in Python 2.7?
I tried now this:
(but it don't work)
with gzip.open(sys.argv[5] + ".json.gz", mode="w") as outfile:
outfile = io.TextIOWrapper(outfile, encoding="utf-8")
json.dump(fdata, outfile, indent=2, ensure_ascii=False)
TypeError: must be unicode, not str
What can i do?
Those are the Python 3 docs. The Python 2 version of gzip does not allow encoding= as a keyword argument to gzip.open().
Seems the question has been answered sufficiently, but for your peace of mind: Alternatively to ensure that Python2 uses utf-8 as standard perhaps try the following, as it then becomes unnecessary to specify an encoding:
import sys
reload(sys)
sys.setdefaultencoding('UTF8')

Confused about __import__ in Python

I trying to import module by __import__ like this:
>>> mod = __import__('x.y.z')
But I only got x:
>>> print mod
>>> <module 'x' from '...'>
How should I do to import z ? I tried like this, it works but i don't know why.
>>> mod = __import__('x.y.z', {}, {}, [''])
>>> print mod
>>> <module 'x.y.z' from '...'>
I'm really confused about this, and I also have no idea with the globals and locals parameters.
Thx a lot!
Relevant notes from the docs (__import__):
When the name variable is of the form package.module, normally, the top-level package (the name up till the first dot) is returned, not the module named by name. However, when a non-empty fromlist argument is given, the module named by name is returned.
Hence, it's similar to writing import x.y.z which also makes x available (as well as x.y and x.y.z).
Use the importlib module instead. Of which the bare bones are made available in 2.7.
import importlib
z = importlib.import_module("z", "x.y")
# equivalent to
from x.y import z