Create a dictionary from the ouput of the program - python-3.x

This is a Python Program to get all the captions from youtube link:
from pytube import YouTube
yt = YouTube('https://youtu.be/5MgBikgcWnY')
captions = yt.captions.all()
for caption in captions:
print(caption)
and the output of the above program is:
<Caption lang="Arabic" code="ar">
<Caption lang="Chinese (China)" code="zh-CN">
<Caption lang="English" code="en">
<Caption lang="English (auto-generated)" code="a.en">
<Caption lang="French" code="fr">
<Caption lang="German" code="de">
<Caption lang="Hungarian" code="hu">
<Caption lang="Italian" code="it">
But I want to get only the lang and code from the above output in a dictionary pair.
{"Arabic" : "ar", "Chinese" : "zh-CN", "English" : "en",
"French : "fr", "German" : "de", "Hungarian" : "hu", "Italian" : "it"}
Thanks in Advance.

It's pretty simple
from pytube import YouTube
yt = YouTube('https://youtu.be/5MgBikgcWnY')
captions = yt.captions.all()
captions_dict = {}
for caption in captions:
# Mapping the caption name to the caption code
captions_dict[caption.name] = caption.code
If you want a one-liner
captions_dict = {caption.name: caption.code for caption in captions}
Output
{'Arabic': 'ar', 'Bangla': 'bn', 'Burmese': 'my', 'Chinese (China)': 'zh-CN',
'Chinese (Taiwan)': 'zh-TW', 'Croatian': 'hr', 'English': 'en',
'English (auto-generated)': 'a.en', 'French': 'fr', 'German': 'de',
'Hebrew': 'iw', 'Hungarian': 'hu', 'Italian': 'it', 'Japanese': 'ja',
'Persian': 'fa', 'Polish': 'pl', 'Portuguese (Brazil)': 'pt-BR',
'Russian': 'ru', 'Serbian': 'sr', 'Slovak': 'sk', 'Spanish': 'es',
'Spanish (Spain)': 'es-ES', 'Thai': 'th', 'Turkish': 'tr',
'Ukrainian': 'uk', 'Vietnamese': 'vi'}

Related

Font - math mode in python

I used this for font:
\usepackage{amsmath} \usepackage{fdsymbol}"
params = {'text.usetex' : True,
'font.size' : 20,
'font.family' : 'lcmodern',
}
plt.rcParams.update(params)
and this for text - LaTeX math mode
ax1.set_xlabel(r'$T$ [s]')
How to set a font of math mode to something like Arial, Trebuchet...?
After advice:
It does not work for ticks.
I tried:
plt.rcParams['text.latex.preamble'] = r"\usepackage{bm} \usepackage{amsmath} \usepackage{fdsymbol} \renewcommand{\familydefault}{\sfdefault}"
params = {'text.usetex' : True,
'font.size' : 20,
}
plt.rcParams.update(params)
and
plt.rcParams['text.latex.preamble'] = r"\usepackage{bm} \usepackage{amsmath} \usepackage{fdsymbol} \usepackage{sansmath} \renewcommand{\familydefault}{\sfdefault}"
params = {'text.usetex' : True,
'font.size' : 20,
}
plt.rcParams.update(params)

API to pull the list of supported languages in AWS Translate

I am currently working on a project where I need to translate Customer comments into English from the source language on AWS. It is easy to do so using AWS Translate but before I call translate API to translate text into English, I want to check whether the source language is supported by AWS or not?
One solution is to put all the language codes supported by AWS Translate into a list and then check source language against this list. This is easy but it is going to be messy and I want to make it more dynamic.
So, I am thinking to code like this
import boto3
def translateUserComment(source_language):
translate = boto3.client(service_name='translate', region_name='region', use_ssl=True)
languages_supported = tanslate.<SomeMethod>()
if source_language in languages_supported:
result = translate.translate_text(Text="Hello, World",
SourceLanguageCode=source_language, TargetLanguageCode="en")
print('TranslatedText: ' + result.get('TranslatedText'))
print('SourceLanguageCode: ' + result.get('SourceLanguageCode'))
print('TargetLanguageCode: ' + result.get('TargetLanguageCode'))
else:
print("The source language is not supported by AWS Translate")
Problem is that I am not able to find out any API call to get the list of languages/language codes supported by AWS Translate for place.
Before I posted this question,
I have tried to search for similar questions on stackoverflow
I have gone through the AWS Translate Developer guide but still no luck
Any suggestion/ redirection to the right approach is highly appreciated.
Currently there is no API for this service, although this code would work, in this code a class is created Translate_lang with all the language codes and country wise
from here-> https://docs.aws.amazon.com/translate/latest/dg/what-is.html
, you can call this class into your program and use it by creating an instance of the class:
translate_lang_check.py
class Translate_lang:
def __init__(self):
self.t_lang = {'Afrikaans': 'af', 'Albanian': 'sq', 'Amharic': 'am',
'Arabic': 'ar', 'Armenian': 'hy', 'Azerbaijani': 'az',
'Bengali': 'bn', 'Bosnian': 'bs', 'Bulgarian': 'bg',
'Catalan': 'ca', 'Chinese (Simplified)': 'zh',
'Chinese (Traditional)': 'zh-TW', 'Croatian': 'hr',
'Czech': 'cs', 'Danish': 'da ', 'Dari': 'fa-AF',
'Dutch': 'nl ', 'English': 'en', 'Estonian': 'et',
'Farsi (Persian)': 'fa', 'Filipino Tagalog': 'tl',
'Finnish': 'fi', 'French': 'fr', 'French (Canada)': 'fr-CA',
'Georgian': 'ka', 'German': 'de', 'Greek': 'el', 'Gujarati': 'gu',
'Haitian Creole': 'ht', 'Hausa': 'ha', 'Hebrew': 'he ', 'Hindi': 'hi',
'Hungarian': 'hu', 'Icelandic': 'is', 'Indonesian': 'id ', 'Italian': 'it',
'Japanese': 'ja', 'Kannada': 'kn', 'Kazakh': 'kk', 'Korean': 'ko',
'Latvian': 'lv', 'Lithuanian': 'lt', 'Macedonian': 'mk', 'Malay': 'ms',
'Malayalam': 'ml', 'Maltese': 'mt', 'Mongolian': 'mn', 'Norwegian': 'no',
'Persian': 'fa', 'Pashto': 'ps', 'Polish': 'pl', 'Portuguese': 'pt',
'Romanian': 'ro', 'Russian': 'ru', 'Serbian': 'sr', 'Sinhala': 'si',
'Slovak': 'sk', 'Slovenian': 'sl', 'Somali': 'so', 'Spanish': 'es',
'Spanish (Mexico)': 'es-MX', 'Swahili': 'sw', 'Swedish': 'sv',
'Tagalog': 'tl', 'Tamil': 'ta', 'Telugu': 'te', 'Thai': 'th',
'Turkish': 'tr', 'Ukrainian': 'uk', 'Urdu': 'ur', 'Uzbek': 'uz',
'Vietnamese': 'vi', 'Welsh': 'cy'}
def check_lang_in_translate(self, given_country):
if given_country in self.t_lang:
return self.t_lang[given_country]
else:
return None
def check_lang_code_in_translate(self, given_lang):
if given_lang in list(self.t_lang.values()):
return True
else:
return False
You can call and check your lang codes using the methods of this class:
from translate_lang_check import Translate_lang
tl = Translate_lang()
print(tl.check_lang_code_in_translate('en'))

AttributeError: module 'csv' has no attribute 'DictWriter'

I keep getting an attribute error from the following code.
import csv
myFile = open('countries.csv', 'w')
with myFile:
myFields = ['country', 'capital']
writer = csv.DictWriter(myFile, fieldnames=myFields)
writer.writeheader()
writer.writerow({'country' : 'France', 'capital': 'Paris'})
writer.writerow({'country' : 'Italy', 'capital': 'Rome'})
writer.writerow({'country' : 'Spain', 'capital': 'Madrid'})
writer.writerow({'country' : 'Russia', 'capital': 'Moscow'})
any ideas

resolved : JqGrid client side searching when user types in in single search box ( below is working grid)

I have used solution and code from other member's answer . thanks for their help . here is working grid : http://jsfiddle.net/x6t24u8s/1/
I am very new to jqgrid and I am looking filtering to work on client side in a single search box as user types in . I'll get data one time from server and then filter in local .
Here is my jsfiddle ( I have took example from other questions)
but it doesn't seem to be working .
Can someone please point to me in right direction .
Top 2 lines define text box and table for list . below is code for jqgrid .
http://jsfiddle.net/2b1yta0j/5/
<input type="text" name="a" id="searchText" value="test2"/>
<table id="list"></table>
var mydata = [
{id:"1",invdate:"2007-10-01",name:"aest",note:"note",amount:"200.00",tax:"10.00",total:"210.00"},
{id:"2",invdate:"2007-10-02",name:"best2",note:"note2",amount:"300.00",tax:"20.00",total:"320.00"},
{id:"3",invdate:"2007-09-01",name:"cest3",note:"note3",amount:"400.00",tax:"30.00",total:"430.00"},
{id:"4",invdate:"2007-10-04",name:"dest",note:"note",amount:"200.00",tax:"10.00",total:"210.00"},
{id:"5",invdate:"2007-10-05",name:"eest2",note:"note2",amount:"300.00",tax:"20.00",total:"320.00"},
{id:"6",invdate:"2007-09-06",name:"fest3",note:"note3",amount:"400.00",tax:"30.00",total:"430.00"},
{id:"7",invdate:"2007-10-04",name:"gest",note:"note",amount:"200.00",tax:"10.00",total:"210.00"},
{id:"8",invdate:"2007-10-03",name:"hest2",note:"note2",amount:"300.00",tax:"20.00",total:"320.00"},
{id:"9",invdate:"2007-09-01",name:"iest3",note:"note3",amount:"400.00",tax:"30.00",total:"430.00"},
{id:"10",invdate:"2007-10-01",name:"jest",note:"note",amount:"200.00",tax:"10.00",total:"210.00"},
{id:"11",invdate:"2007-10-02",name:"xest2",note:"note2",amount:"300.00",tax:"20.00",total:"320.00"},
{id:"12",invdate:"2007-09-01",name:"yest3",note:"note3",amount:"400.00",tax:"30.00",total:"430.00"},
{id:"13",invdate:"2007-10-04",name:"zest",note:"note",amount:"200.00",tax:"10.00",total:"210.00"},
{id:"14",invdate:"2007-10-05",name:"aest2",note:"note2",amount:"300.00",tax:"20.00",total:"320.00"},
{id:"15",invdate:"2007-09-06",name:"best3",note:"note3",amount:"400.00",tax:"30.00",total:"430.00"},
{id:"16",invdate:"2007-10-04",name:"cest",note:"note",amount:"200.00",tax:"10.00",total:"210.00"},
{id:"17",invdate:"2007-10-03",name:"dest2",note:"note2",amount:"300.00",tax:"20.00",total:"320.00"},
{id:"18",invdate:"2007-09-01",name:"eest3",note:"note3",amount:"400.00",tax:"30.00",total:"430.00"}
];
var grid = $("#list");
grid.jqGrid({
data: mydata,
datatype: "local",
colNames:['Inv No','Date', 'Client', 'Amount','Tax','Total','Notes'],
colModel:[
{name:'id',index:'id', key: true, width:70, sorttype:"int"},
{name:'invdate',index:'invdate', width:90, sorttype:"date"},
{name:'name',index:'name', width:100},
{name:'amount',index:'amount', width:80, align:"right",sorttype:"float"},
{name:'tax',index:'tax', width:80, align:"right",sorttype:"float"},
{name:'total',index:'total', width:80,align:"right",sorttype:"float"},
{name:'note',index:'note', width:150, sortable:false}
],
pager:'#pager',
rowNum: 10,
rowList: [5, 10, 20, 50],
sortname: 'id',
sortorder: 'asc',
viewrecords: true,
height: "100%",
caption: "Single search",
loadonce : "true"
});
grid.jqGrid('navGrid','#pager',{add:false,del:false,search:true,refresh:false});
$("#searchText").on('keypress',function(){
var text = $("#searchText").val();
var postdata = grid.jqGrid('getGridParam','postData');
$.extend(postdata,{filters:'',searchField: 'name', searchOper: 'bw', searchString: text});
grid.jqGrid('setGridParam', { search: text.length>0, postData: postdata });
grid.trigger("reloadGrid",[{page:1}]);
});

Nested list with TAL

I'm using Chameleon in the Pyramids Framework and want to repeat nested list while rendering the template.
My minimized HTML-Code is:
1. <ul>
2. <li tal:repeat="item items">
3. <input tal:attributes="id item.id; onclick item.url">
4. <label tal:repeat="premise item.values" tal:attributes="for item.id; id premise.id">
5. <label tal:replace="premise.title"/>
6. </label>
7. <label tal:attributes="for item.id" tal:content="item.title"/>
8. </li>
9. </ul>
Whereby I got the following json-Data
[{
'url': 'location.href="http://..."',
'values':
[{
'id': '70',
'title': 'some title 1'
}],
'attitude': 'justify',
'id': '68',
'title': 'some title 2'
}, {
'url': 'null',
'values':
[{
'id': '0',
'title': 'some title 3!
}],
'attitude': 'justify',
'id': '0',
'title':
'some title 4'
}]
If I kill HTML-lines 4.-6., everything is fine, otherwise Pyramid/Chameleon throws:
File "/usr/local/lib/python3.4/dist-packages/chameleon/tal.py", line 471, in __call__
iterable = list(iterable) if iterable is not None else ()
TypeError: 'builtin_function_or_method' object is not iterable
Anyone some idea?
Thanks to #pyramid in the IRC i got the first hint, which is mentioned in the comment. But..never ever name a key 'value' or 'values'!

Resources