Hex to integer when reading MIDI file

This might be a simple question. I'm reading a binary MIDI file using Python 3 and I'm having problems understanding how to turn the hex value \x00\x00\x00\x06 into an integer, as I don't know what to do with the slash \. To get this value I'm using ...
more »

2017-04-20 03:04 (1) Answers

XML to CSV using Python

I have XML file which I want to convert to CSV using Python. I need contents from the Testitemname tag as CSV headers and contents in the Testvalue tag as values in CSV. Can some one help me with this? Sample XML file (input) <sample:batch xmln...
more »

2017-04-19 23:04 (1) Answers

What is python .. ("dot dot") notation syntax?

I recently came across a syntax I never seen before when I learned python nor in most tutorials, the .. notation, it looks something like this: f = 1..__truediv__ # or 1..__div__ for python 2 print(f(8)) # prints 0.125 I figured it was exactly t...
more »

2017-04-19 08:04 (4) Answers

How to modify full text of some columns in pandas

I got a fairly big dataframe from a csv in pandas. The problem is that on some columns I get strings of text which I would like to isolate the last character to turn it into integers. I found a solution, but I am fairly sure it's not the most effic...
more »

2017-04-18 23:04 (2) Answers

Array slicing with booleans as indices

Can someone explain this syntax to me? I've searched through docs/blogs but can't find any mention of using booleans as indices for array slicing. I found this syntax in this script convert_base.py: is_negative = num_as_string[0] == '-' num_as_str...
more »

2017-04-18 23:04 (2) Answers

test_venv failed Python 3.6.1 install

I've just installed Linux Mint 18.1 (so I'm new to Linux and I don't know what I'm doing!) and I'm attempting to install Python 3.6.1. However, one of the install tests keeps failing--test_venv. Here's the log: running build running build_ext INFO: ...
more »

2017-04-16 21:04 (1) Answers

Can't acces a global variable inside a method

I have a problem that when i try to use a global variable inside a method, an error is produced ("local variable 'b' referenced before assignment"). Why is this not the case when the variable is an element of a list? I'm pretty new to python so pleas...
more »

2017-04-16 15:04 (2) Answers

Open the 'save file' option in Python

How do you open the dialog box that asks where to save a file? I can open windows explorer, but not the box that allows a user to select a save location, like shown below. If it makes any difference, I would only be saving text files. I am trying ...
more »

2017-04-15 17:04 (1) Answers

How to find Run length encoding in python

I have an array ar = [2,2,2,1,1,2,2,3,3,3,3]. For this array, I want to find the lengths of consecutive same numbers like: values: 2, 1, 2, 3 lengths: 3, 2, 2, 4 In R, this is obtained by using rle() function. Is there any existing function in py...
more »

2017-04-15 12:04 (1) Answers

Add elements to an array with a probability

So I am building a list in Python, for example, let us say the first 100 integers, but I do need all the 100 integers but only a sample lets say 3. import random def f(): list_ = [] for i in range(100): list_.append(i) return ...
more »

2017-04-13 03:04 (1) Answers

Efficiently sum items by type

I have a list of items with properties "Type" and "Time" that I want to quickly sum the time for each "Type" and append to another list. The list looks like this: Items = [{'Name': A, 'Type': 'Run', 'Time': 5}, {'Name': B, 'Type': 'Walk'...
more »

2017-04-12 20:04 (8) Answers

Can't install aiohttp==2.0.6 on Alpine Linux v3.5

I'm using a fresh install of Alpine Linux v3.5 with edge/main repos These are the commands I'm running: $ echo "http://mirror.yandex.ru/mirrors/alpine/edge/main" > /etc/apk/repositories $ apk update $ apk add python3 $ pip3 install aiohttp==2.0....
more »

2017-04-11 18:04 (1) Answers

How to use django-tls in Django unit tests

I'm trying to write some unit tests on a Django (I'm using Django==1.9.12) application that uses django-tls, but I always get this exception: RuntimeError: no object bound to request. My code looks like this: settings.py: As in django-tls doc I add...
more »

2017-04-10 20:04 (0) Answers

Django objects.all() doesn`t display any content

Hi everybody! Im just starting a way of django programming so sometimes really get confused. I`m trying to display all my objects from DB, but when opening the page its simply empty. There are content added and I tried ListView previously and it wo...
more »

2017-04-09 23:04 (1) Answers

Stop a python script from executing

I have the below python script(server.py) in order to listen to a port and capture the requests from the client. I am calling this script from another python file(Main.py). My requirement is to stop executing server.py after certain amount of time. I...
more »

2017-04-09 20:04 (1) Answers

Get a random sample with repetition

I have this list: colors = ["R", "G", "B", "Y"] and I want to get 4 random letters from it, but including repetition. Running this will only give me 4 unique letters, but never any repeating letters: print(random.sample(colors,4)) How do I get...
more »

2017-04-07 17:04 (3) Answers

List of dict unique combination Python

I have big data For the visualization purpose, This is the example list of dict as, Input : a = [{'e1': 'a','e2': 'b'}, {'e1': 'b','e2': 'a'}, {'e1': 'a','e2': 'c'} ] Output : a = [{'e1': 'a','e2': 'b'}, {'e1': 'a','e2': 'c'}] Detail: If {'...
more »

2017-04-07 07:04 (2) Answers

Python joining list elements in a tricky way

So, I have this list l = ['abc', 'retro', '', '', 'images', 'cool', '', 'end'] and, I want to join them in a way such as: l = ['abc retro', '', '', 'images cool', '', 'end'] I tried lots of methods but nothing seemed to work. Any suggestions? ...
more »

2017-04-06 12:04 (5) Answers

Django fails to find psycopg2 module

I am installing python 3.5, django 1.10 and psycopg2 2.7.1 on Amazon EC2 server in order to use a Postgresql database. I am using python 3 inside a virtual environment, and followed the classic installation steps: cd /home/mycode virtualenv-3.5 p3en...
more »

2017-04-06 12:04 (1) Answers