Skip to main content

Check the license in the Youtube video through API [StackOverflow answer]

This is my answer to the trouble shooting question about getting the license information from the Youtube API:

@goodvibration answer was correct, as long as you keep into account that the json.load uses the read function, which means that once the data is read, it can not be read again. In this case it would return empty byte string.

This code works by printing True at the end.

response = urllib2.urlopen('') for item in json.load(response)['items']: print(item['status']['license'] == 'creativeCommon')

Also, in your original example and in your example of error you used list(json.load(response)) when saving to data. This would mean, that you don't get the entire json, but just the keys. So in your case, I would suggest to not change the response to list. But since you did not use the data variable later in example, it does not really change the results. But it could be important, if you have more information you want to check/save.

Also, in this case, because the json.load() uses the read function, which can not be used multiple times, you need to save the entire json and then read from it. The code would then be:

response = urllib2.urlopen('') data = json.load(response) for item in data['items']: print(item['status']['license'] == 'creativeCommon')

You can see this if you try and read the response that you get. You can put the following code to test it:

response = urllib2.urlopen('') print( print(

In this case the results would be:

b'{\n "kind": "youtube#videoListResponse",\n "etag": "\"Bdx4f4ps3xCOOo1WZ91nTLkRZ_c/3jdRB-NXSAfUQj7e_FmBbivkK1o\"",\n "pageInfo": {\n "totalResults": 1,\n "resultsPerPage": 1\n },\n "items": [\n {\n "kind": "youtube#video",\n "etag": "\"Bdx4f4ps3xCOOo1WZ91nTLkRZ_c/NUd32t1_moLGAwVuu-ZujlkaiWM\"",\n "id": "gwLej8heN5c",\n "status": {\n "uploadStatus": "processed",\n "privacyStatus": "public",\n "license": "creativeCommon",\n "embeddable": true,\n "publicStatsViewable": true\n }\n }\n ]\n}\n' b''

In the first case, the result is a json in byte-string form, while the second one is an empty byte-string. So when you would try to use multiple json.load() on the same response element, you would get the JSONDecodeError (same as in your comment), since the second time there is no json to parse.

Sortying list of lists based on multiple criteria in Python [StackOverflow answer]

My answer to the following StackOverflow question: They wanted to sort based on column 5, and then based on column 8, with entries that have value 0 being listed first.

If you use data.sort(key = lambda x:x[0] == 8), then it will sort every one with 8 on the position 0 to the last place. So what you could do would be the following:

First sort them, so all the 8 at the position 0 are on the end:

data.sort(key = lambda x:x[0] == 8)

Then sort based on the position 5:

data.sort(key = lambda x:x[5])

And then reverse the order:


Pivot table sorting in Pandas [StackOverflow]

The answer to the question about the sorting based on the difference in the items with the same value in one column:

First you need to calculate the differences for each item type. One of the ways, how to do this with pandas would be to use pivot_tables. Here you tell it which dataframe (df), based on which columns to calculate (values="TotalCost"), what function to use to calculate it (aggfunc=np.diff) and how to group them (index=["ItemType"]).

diff = pandas.pivot_table(df, values="TotalCost", index=["ItemType"], aggfunc=np.diff)

You case above only have 2 possible months. If you had more than two, then np.diff would give you values in a list. In this case you have two options. Either you filter the data frame, so there are only two months in it. This can be done like this:

df = df[[a or b for a, b in zip(df["Year_Month"] == "Jul-2017", df["Year_Month"] == "Jun-2017")]]

The other option is that you calculate the mean difference in months. This can be done with the following function, which you would then replace np.diff with:

def mean_diff(l): return np.mean(np.diff(l))

Then you can use this to calculate the difference for each element:

df["Diff"] = [float(diff.loc[d]) for d in df["ItemType"]]

After that, you just sort by the difference (and then by item, in case there are multiple items with the same difference)

df.sort_values(by=["Diff", "ItemType", "Year_Month"]).drop(columns = 'Diff')

Why Our Parliament is not the Only Weird One

Sometimes it might seems like, our parliament is a really weird one. You don't believe me?

I remember once watching a parliamentary session, where the person had a couple minutes talk about, how there is the smell of glue. And that maybe there is a lot og glue in the air. So they all might have ended up high. Which, according to that speaker, would explain why people are giving the suggestions they do.

Or a more recent one. There was a meeting. A person admitted on the camera, that they stole a sandwich. Called it a social experiment... well you can imagine what happened in the end.

So, in order to counteract these, I am going to list some of the thing that are weird in the British parliament:

  • They sit 2 sword lengths apart, so that they are unable to cut opponent head off
  • They can't have a debate without a golder mace listening to them
  • They can not address each other directly (I guess they invented passive-aggressiveness?)
  • They are not allowed to say, that something is not true
  • They vote by walking to the room

Topic Modeling of Python Conversations on Tumblr

I had presented a lightning talk on May Python Meetup.

What I did was take all the Python tagged posts from Tumblr. Then I topic analysed them and tried to figure out some interesting things.

Here are the interesting things:

  1. Python is connected to three big topics: nature (the dark night with a dog barking one), startups and coding.

  2. When ignoring the nature one, the trends show that from 2013 to 2016 there was a lot of interest in learning python. But then the interest shifted to startups. (Not sure why. Would be interested in finding out...)

  1. There are people posting python code with comments in Japanese. So, a Japanese python community? (not sure, why it is on Tumblr...)

  2. People really like to create chat apps. (again Why???)

My slides and code are also available.