Text To Speech (Multi Lang) Using gTTS API In Python - python

I am actually trying to make a text to speech code where the text which is written in any language will be spoken out loud. The language in which it will speak should be selected by the user in a dropdown. I have written the code which I will be showing below.
import gtts
import os
import playsound
import googletrans
from googletrans import Translator, LANGUAGES
import tkinter as tk
root=Tk()
root.title("TEXT TO SPEECH")
root.geometry("1280x720")
root.resizable(False,False)
root.configure(bg="#305065")
#Top Text
Label(root,text="TEXT TO SPEECH",font="Helvetica 20 bold",fg="white",bg='#305065').pack(pady=10)
a=tk.StringVar()
auto_select=ttk.Combobox(frame1,width=27,textvariable=a,state='readonly',font=('verdana',10,'bold'))
auto_select['values'] = (
'Afrikaans',
'Albanian',
'Amharic',
'Arabic',
'Armenian',
'Azerbaijani',
'Basque',
'Belarusian',
'Bengali',
'Bosnian',
'Bulgarian',
'Batalan',
'Cebuano',
'Chichewa',
'Chinese (simplified)',
'Chinese (traditional)',
'Corsican',
'Croatian',
'Czech',
'Danish',
'Dutch',
'English',
'Esperanto',
'Estonian',
'Filipino',
'Finnish',
'French',
'Frisian',
'Galician',
'Georgian',
'German',
'Greek',
'Gujarati',
'Gaitian creole',
'Hausa',
'Hawaiian',
'Hebrew',
'Hebrew',
'Hindi',
'Hmong',
'Hungarian',
'Icelandic',
'Igbo',
'Indonesian',
'Irish',
'Italian',
'Japanese',
'Javanese',
'Kannada',
'Kazakh',
'Khmer',
'Korean',
'Kurdish (kurmanji)',
'Kyrgyz',
'Lao',
'Latin',
'Latvian',
'Lithuanian',
'Luxembourgish',
'Macedonian',
'Malagasy',
'Malay',
'Malayalam',
'Maltese',
'Maori',
'Marathi',
'Mongolian',
'Myanmar (burmese)',
'Nepali',
'Norwegian',
'Odia',
'Pashto',
'Persian',
'Polish',
'Portuguese',
'Punjabi',
'Romanian',
'Russian',
'Samoan',
'Scots gaelic',
'Serbian',
'Sesotho',
'Shona',
'Sindhi',
'Sinhala',
'Slovak',
'Slovenian',
'Somali',
'Spanish',
'Sundanese',
'Swahili',
'Swedish',
'Tajik',
'Tamil',
'Telugu',
'Thai',
'Turkish',
'Ukrainian',
'Urdu',
'Uyghur',
'Uzbek',
'Vietnamese',
'Welsh',
'Xhosa',
'Yiddish',
'Yoruba',
'Zulu',
)
auto_select.place(x=150,y=60)
#Textboxes & Buttons
sor_txt=Text(frame1,width=40,height=11,borderwidth=5,relief=RIDGE,font=('verdana',15))
sor_txt.place(x=30,y=110)
dest_txt=Text(frame1,width=40,height=11,borderwidth=5,relief=RIDGE,font=('verdana',15))
dest_txt.place(x=710,y=110)
button_change=Button(frame1,text="TRANSLATE",relief=RAISED,borderwidth=2,font=('verdana',10,'bold'),bg='#248aa2',cursor="hand2",command=translate)
button_change.place(x=590,y=430,width=100,height=40)
button_clear=Button(frame1,text="CLEAR",relief=RAISED,borderwidth=2,font=('verdana',10,'bold'),bg='#248aa2',cursor="hand2",command=clear)
button_clear.place(x=590,y=480,width=100,height=40)
#Translator_Code
def translate():
lang_1=sor_txt.get(1.0,END)
cl=choose_lang.get()
if lang_1 == '':
messagebox.showerror("TEXT TO SPEECH","Enter the text to translate!")
else:
dest_txt.delete(1.0,END)
translator=Translator()
output=translator.translate(lang_1,dest=cl)
dest_txt.insert('end',output.text)
dest_audio = gtts.gTTS(output.text, lang=cl)
playsound.playsound(dest_audio)
def clear():
sor_txt.delete(1.0,'end')
dest_txt.delete(1.0,'end')
root.mainloop()
After executing this code I am getting a error where it is saying language not found. So may someone tell me who can I solve this?
Screenshot of my Error: https://i.stack.imgur.com/6mk5A.png

Check out the supported languages here: https://cloud.google.com/text-to-speech/docs/voices
for example "Amharic" isn't a supported language so simply fix the list of languages by removing unsupported languages

Related

Removing stop words from string using spacy in diffrent languages

I have an array of strings in different languages and I would like to remove stop words from these strings.
example of string :
["mai fostul președinte egiptean mohamed morsi ", "em bon jovi lançou o álbum have a nice day a ", " otok škulj är en ö i kroatien den ligger i län"...]
this is the list of languages I'm willing to use :
['French',
'Spanish',
'Thai',
'Russian',
'Persian',
'Indonesian',
'Arabic',
'Pushto',
'Kannada',
'Danish',
'Japanese',
'Malayalam',
'Latin',
'Romanian',
'Swedish',
'Portugese',
'English',
'Turkish',
'Tamil',
'Urdu',
'Korean',
'German',
'Greek',
'Italian',
'Chinese',
'Dutch',
'Estonian',
'Hindi']
I am using Spacy library, but I'm looking for something that support multiple languages.
what I have tried already:
import pandas as pd
import nltk
nltk.download('punkt')
import spacy
nlp = spacy.load("xx_ent_wiki_sm")
from spacy.tokenizer import Tokenizer
from nltk.corpus import stopwords
from nltk.tokenize import word_tokenize
doc = nlp("This is a sentence about Facebook.")
print([(ent.text, ent.label) for ent in doc.ents])
all_stopwords = nlp.Defaults.stop_words
all_stopwords = nlp.Defaults.stop_words
data_text=df1['Text'] #here where i store my strings
for x in data_text:
text_tokens = word_tokenize(x)
tokens_without_sw=[word for word in text_tokens if not word inall_stopwords]
print(tokens_without_sw)

Make a ReportLab PDF interactive - Unable to reference widgets on PDF

I am currently building a ReportLab PDF Form which I want to make interactive. I want that the options shown on the second choice widget, depend on the selection of the first choice widget. (For example: if the selection of the first choice is 'Italy', show as food options on the second widget 'Pizza' and 'Spaghetti').
Currently I am trying to control such condition with an if, however I have been unable to reference the first widget correctly. (Since I called it name='choice1', I figured this would be the way of calling such widget). I get an error:
if choice1 == 'Italy':
NameError: name 'choice1' is not defined
Is there a correct way to do the desired action? I am looking at the Reportlab documentation, however I have not found an answer.
My current Python code is the following:
from reportlab.pdfgen.canvas import Canvas
from datetime import datetime, timedelta
from reportlab.platypus import Table, TableStyle
from reportlab.lib import colors
from reportlab.pdfbase import pdfform
import win32com.client as win32
import pathlib
file_name = 'Example.pdf'
document_title = 'Example'
title = 'Example File'
instrucciones_1 = 'INSTRUCCIONES: Llenar toda la solicitud con la información que se pide.'
instrucciones_2 = 'Al finalizar, este PDF se enviará automáticamente al encargado de su área para aprobación.'
instrucciones_3 = 'La aprobación queda sujeta al criterio de dicho encargado. '
canvas = Canvas(file_name)
canvas.setTitle(document_title)
canvas.setFont("Helvetica-Bold", 24)
canvas.drawCentredString(385, 795, title)
now = datetime.now()
full_date = f'{now.day}/{now.month}/{now.year}'
canvas.setFont('Helvetica', 16)
canvas.drawCentredString(350, 790-20, 'Fecha solicitud: ')
canvas.setFont('Helvetica-Bold', 16)
canvas.drawCentredString(450, 790-20, full_date)
canvas.line(35, 755, 565, 755)
canvas.setFont("Helvetica", 12)
canvas.drawString(40, 740, instrucciones_1)
canvas.drawString(40, 740-15, instrucciones_2)
canvas.drawString(40, 740-30, instrucciones_3)
canvas.line(35, 740 - 35, 565, 740 - 35)
form = canvas.acroForm
canvas.setFont('Helvetica', 14)
canvas.drawString(70, 675, 'Origin of Food: ')
options = ['Select origin', 'Italy', 'Mexico', 'US']
form.choice(name='choice1', tooltip='Select origin of food',
value='Select origin',
x=165, y=670, width=200, height=25,
borderColor=colors.blue, fillColor=colors.lightblue,
textColor=colors.black, forceBorder=True, options=options)
origin = ''
food_italy = ['Select food', 'Spaghetti', 'Pizza']
food_mexico = ['Select food', 'Chiles en Nogada', 'Tacos]
food_us = ['Select food', 'Burgers', 'Buffalo Wings']
if choice1.value == 'Italy':
sucursal = food_italy
elif choice1.value == 'Mexico':
sucursal = food_mexico
elif choice1.value == 'US':
sucursal = food_us
canvas.drawString(70, 645, 'Food: ')
form.choice(name='choice2', tooltip='Select food',
value='Select food',
x=165, y=670, width=200, height=25,
borderColor=colors.blue, fillColor=colors.lightblue,
textColor=colors.black, forceBorder=True, options=sucursal)
canvas.save()
Any suggestions?
Thanks a lot in advance!
EDIT
If PDF and Reportlab is not the way, is there some way I can do an executable file/form which can perform such tasks? Thanks a lot.
This cannot be done in the way you approach this, i.e. in the Python code. The reason for this is that the selection of e.g. 'Italy' happens only when the user opens the PDF file and selects it. So you would need to create a (deprecated) XFA form for this.

"error: (-2:Unspecified error) Can't create layer" shows while using net.detect in python.how to fix this type problem?

I am facing a problem in classIds, confs, bbox = net.detect(img,confThreshold=float(0.5)).
How to fix it?Please help.Thanks in advance.
import cv2
img= cv2.imread('lena.png')
classNames= []
classFile = 'coco.names'
#coco names er file gula class names e rakha
with open(classFile,'rt') as f:
classNames = f.read().rstrip('\n').split('\n')
print(classNames)
configPath = 'ssd_mobilenet_v3_large_coco_2020_01_14.pbtxt' #configuration path
weightsPath = 'frozen_inference_graph.pb'
net = cv2.dnn_DetectionModel(weightsPath,configPath)
net.setInputSize(320,320)
net.setInputScale(1.0/ 127.5)
net.setInputMean((127.5, 127.5, 127.5))
net.setInputSwapRB(True)
classIds, confs, bbox = net.detect(img,confThreshold=float(0.5))
print(classIds,bbox)
cv2.imshow("Output",img)
cv2.waitKey(0)
C:\Users\User\AppData\Local\Programs\Python\Python37\python.exe "H:/My ML Projects/object detection/test.py"
['person', 'bicycle', 'car', 'motorcycle', 'airplane', 'bus', 'train', 'truck', 'boat', 'traffic light', 'fire hydrant', 'street sign', 'stop sign', 'parking meter', 'bench', 'bird', 'cat', 'dog', 'horse', 'sheep', 'cow', 'elephant', 'bear', 'zebra', 'giraffe', 'hat', 'backpack', 'umbrella', 'shoe', 'eye glasses', 'handbag', 'tie', 'suitcase', 'frisbee', 'skis', 'snowboard', 'sports ball', 'kite', 'baseball bat', 'baseball glove', 'skateboard', 'surfboard', 'tennis racket', 'bottle', 'plate', 'wine glass', 'cup', 'fork', 'knife', 'spoon', 'bowl', 'banana', 'apple', 'sandwich', 'orange', 'broccoli', 'carrot', 'hot dog', 'pizza', 'donut', 'cake', 'chair', 'couch', 'potted plant', 'bed', 'mirror', 'dining table', 'window', 'desk', 'toilet', 'door', 'tv', 'laptop', 'mouse', 'remote', 'keyboard', 'cell phone', 'microwave', 'oven', 'toaster', 'sink', 'refrigerator', 'blender', 'book', 'clock', 'vase', 'scissors', 'teddy bear', 'hair drier', 'toothbrush', 'hair brush']
Traceback (most recent call last):
File "H:/My ML Projects/object detection/test.py", line 22, in <module>
classIds, confs, bbox = net.detect(img,confThreshold=float(0.5))
cv2.error: OpenCV(4.2.0) C:\projects\opencv-python\opencv\modules\dnn\src\dnn.cpp:562: error: (-2:Unspecified error) Can't create layer "FeatureExtractor/MobilenetV3/Conv/hard_swish/add" of type "AddV2" in function 'cv::dnn::dnn4_v20191202::LayerData::getLayerInstance'
Process finished with exit code 1
Try:-
pip install --upgrade opencv-python
Run
pip install --upgrade opencv-python
When I upgraded OpenCV from version 4.2 to 4.4, the error was fixed.
[[1]] [[ 60 40 373 461]]
Process finished with exit code 0
Install opencv library using:
pip install opencv-python
It'll work.
Uninstall opencv and install again with newer version, don't upgrade!
I had same issue and it's help me...
pip install --upgrade opencv-python #upgrade opencv
And restart Jupyter notebook or other editors then run error won't come
print(cv2.__version__) # for checking version
Change cv2.VideoCapture(1) to cap = cv2.VideoCapture(0)

Question about coding association rules for an apriori algorithm in python

I'm just wondering if there is way to display only the "support" and "confidence" columns? I don't need to display the antecedent, consequent, or lift columns.
This is my code below:
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from apyori import apriori
from mlxtend.preprocessing import TransactionEncoder
from mlxtend.frequent_patterns import apriori
from mlxtend.frequent_patterns import association_rules
dataset = [['plates', 'forks', 'knives'],
['plates', 'bowls', 'glasses'],
['forks', 'knives'],
['glasses', 'forks', 'knives'],
['microwave', 'blender'],
['dumbbell', 'workout bands', 'water bottle'],
['dumbbell', 'sneakers'],
['workout bands', 'sneakers'],
['bike', 'earbuds'],
['yoga mat', 'headphones'],
['camera'],
['iPad', 'earbuds', 'phone charger'],
['iPad', 'laptop', 'laptop charger'],
['headphones', 'laptop', 'laptop charger'],
['iPad', 'bluetooth speaker', 'phone charger'],
['microwave', 'coffee maker'],
['camping tent', 'water bottle', 'flashlight'],
['sleeping bag', 'yoga mat', 'sneakers'],
['tv ', 'tv remote'],
['tv', 'tv remote', 'bluetooth speaker']]
te = TransactionEncoder()
te_ary = te.fit(dataset).transform(dataset)
df = pd.DataFrame(te_ary, columns=te.columns_)
frequent_itemsets = apriori(df, min_support=0.1, use_colnames=True)
frequent_itemsets
association_rules(frequent_itemsets, metric="confidence", min_threshold=0.25)
Thank you!
I do not know the library but the API documentation says the return type of the association_rules is a pandas Dataframe.
So you can do standard pandas stuff with it:
df = association_rules(frequent_itemsets, metric="confidence", min_threshold=0.25)
print(df[["support", "confidence"]])

Is there a tool to check what names I have used from a "wildly" imported module?

I've been using python to do computations for my research. In an effort to clean up my terrible code, I've been reading Code Like a Pythonista: Idiomatic Python by David Goodger.
In this article, Goodger advises against "wild-card" imports of the form
from module import *
My code uses a lot of these. I'd like to clean my code up, but I'm not sure how. I'm curious if there is a way to check what names from module I have used in my code. This way I could either explicitly import these names or replace the names with module.name. Is there a tool designed to accomplish such a task?
Use a tool like pyflakes (which you should use anyway) to note which names in your code become undefined after you replace from module import * with import module. Once you've positively identified every instance of a name imported from module, you can assess whether to
Always use import module and module.x for x imported from module.
Use import module as m and m.x if the module name is long.
Selectively import some names from module into the module namespace with from module import x, y, z
The above three are not mutually exclusive; as an extreme example, you can use all three in the same module:
import really.long.module.name
import really.long.module.name as rlmn
from really.long.module.name import obvious_name
really.long.module.name.foo() # full module name
rlmn.bar() # module alias
obvious_name() # imported name
all in the same code. (I don't recommend using all three in the same module. Stick with either the full module name or the alias throughout a single module, but there is no harm importing common, obvious names directly and using the fully qualified name for more obscure module attributes.)
Overview
One approach is to:
Manually (or even automatically) identify each of the modules you've imported from using the * approach,
Import them in a separate file,
And then do a search-and-replace if they appear in sys.modules[<module>].__dict__, which keeps track of which objects from a Python module have been loaded.
See for yourself sys.modules in action:
from numpy import *
import sys
sys.modules['numpy'].__dict__.keys() # will display everything you just imported from `numpy`
>>> ['disp', 'union1d', 'all', 'issubsctype', 'savez', 'atleast_2d', 'restoredot', 'ptp', 'PackageLoader', 'ix_', 'mirr', 'blackman', 'FLOATING_POINT_SUPPORT', 'division', 'busdaycalendar', 'pkgload', 'void', 'ubyte', 'moveaxis', 'ERR_RAISE', 'void0', 'tri', 'diag_indices', 'array_equal', 'fmod', 'True_', 'indices', 'loads', 'round', 'set_numeric_ops', 'pmt', 'nanstd', '_mat', 'cosh', 'object0', 'argpartition', 'FPE_OVERFLOW', 'index_exp', 'append', 'compat', 'nanargmax', 'hstack', 'typename', 'diag', 'rollaxis', 'ERR_WARN', 'polyfit', 'version', 'memmap', 'nan_to_num', 'complex64', 'fmax', 'spacing', 'sinh', '__git_revision__', 'unicode_', 'sinc', 'trunc', 'vstack', 'ERR_PRINT', 'asscalar', 'copysign', 'less_equal', 'BUFSIZE', 'object_', 'divide', 'csingle', 'dtype', 'unsignedinteger', 'fastCopyAndTranspose', 'bitwise_and', 'uintc', 'select', 'deg2rad', 'nditer', 'eye', 'kron', 'newbuffer', 'negative', 'busday_offset', 'mintypecode', 'MAXDIMS', 'sort', 'einsum', 'uint0', 'zeros_like', 'int_asbuffer', 'uint8', 'chararray', 'linspace', 'resize', 'uint64', 'ma', 'true_divide', 'Inf', 'finfo', 'triu_indices', 'complex256', 'add_newdoc', 'seterrcall', 'logical_or', 'minimum', 'WRAP', 'tan', 'absolute', 'MAY_SHARE_EXACT', 'numarray', 'array_repr', 'get_array_wrap', 'polymul', 'tile', 'array_str', 'setdiff1d', 'sin', 'longlong', 'product', 'int16', 'str_', 'mat', 'fv', 'max', 'asanyarray', 'uint', 'npv', 'logaddexp', 'flatnonzero', 'amin', 'correlate', 'fromstring', 'left_shift', 'searchsorted', 'int64', 'may_share_memory', 'dsplit', 'intersect1d', 'can_cast', 'ppmt', 'show_config', 'cumsum', 'roots', 'outer', 'CLIP', 'fix', 'busday_count', 'timedelta64', 'degrees', 'choose', 'FPE_INVALID', 'recfromcsv', 'fill_diagonal', 'empty_like', 'logaddexp2', 'greater', 'histogram2d', 'polyint', 'arctan2', 'datetime64', 'complexfloating', 'ndindex', 'ctypeslib', 'PZERO', 'isfortran', 'asfarray', 'nanmedian', 'radians', 'fliplr', 'alen', 'recarray', 'modf', 'mean', 'square', 'ogrid', 'MAY_SHARE_BOUNDS', 'nanargmin', 'r_', 'diag_indices_from', 'hanning', 's_', 'allclose', 'extract', 'float16', 'ulonglong', 'matrix', 'asarray', 'poly1d', 'promote_types', 'rec', 'datetime_as_string', 'uint32', 'math', 'log2', '__builtins__', 'cumproduct', 'diagonal', 'atleast_1d', 'meshgrid', 'column_stack', 'put', 'byte', 'remainder', 'row_stack', 'expm1', 'nper', 'ndfromtxt', 'matmul', 'place', 'DataSource', 'newaxis', 'arccos', 'signedinteger', 'ndim', 'rint', 'number', 'rank', 'little_endian', 'ldexp', 'lookfor', 'array', 'vsplit', 'common_type', 'size', 'logical_xor', 'geterrcall', 'sometrue', 'exp2', 'bool8', 'msort', 'alltrue', 'zeros', 'False_', '__NUMPY_SETUP__', 'nansum', 'bool_', 'inexact', 'nanpercentile', 'broadcast', 'copyto', 'short', 'arctanh', 'typecodes', 'rot90', 'savetxt', 'sign', 'int_', 'std', 'not_equal', 'fromfunction', 'tril_indices_from', '__config__', 'double', 'require', 'rate', 'typeNA', 'str', 'getbuffer', 'abs', 'clip', 'savez_compressed', 'frompyfunc', 'triu_indices_from', 'conjugate', 'alterdot', 'asfortranarray', 'binary_repr', 'angle', 'lib', 'min', 'unwrap', 'apply_over_axes', 'ERR_LOG', 'right_shift', 'take', 'broadcast_to', 'byte_bounds', 'trace', 'warnings', 'any', 'shares_memory', 'compress', 'histogramdd', 'issubclass_', 'multiply', 'mask_indices', 'amax', 'logical_not', 'average', 'partition', 'nbytes', 'exp', 'sum', 'dot', 'int0', 'nanprod', 'longfloat', 'random', 'setxor1d', 'copy', 'FPE_UNDERFLOW', 'frexp', 'errstate', 'nanmin', 'swapaxes', 'SHIFT_OVERFLOW', 'infty', 'fft', 'ModuleDeprecationWarning', 'digitize', '__file__', 'NZERO', 'ceil', 'ones', 'add_newdoc_ufunc', '_NoValue', 'deprecate', 'median', 'geterr', 'convolve', 'isreal', 'where', 'isfinite', 'SHIFT_UNDERFLOW', 'MachAr', 'argmax', 'testing', 'deprecate_with_doc', 'full', 'polyder', 'rad2deg', 'isnan', '__all__', 'irr', 'sctypeDict', 'NINF', 'min_scalar_type', 'count_nonzero', 'sort_complex', 'nested_iters', 'concatenate', 'vdot', 'bincount', 'transpose', 'array2string', 'corrcoef', 'fromregex', 'vectorize', 'set_printoptions', 'isrealobj', 'trim_zeros', 'unravel_index', 'cos', 'float64', 'log1p', 'ushort', 'equal', 'cumprod', 'float_', 'vander', 'geterrobj', 'load', 'fromiter', 'poly', 'bitwise_or', 'polynomial', 'diff', 'iterable', 'array_split', 'get_include', 'pv', 'tensordot', 'piecewise', 'invert', 'UFUNC_PYVALS_NAME', 'SHIFT_INVALID', 'c_', 'flexible', 'pi', '__doc__', 'empty', 'VisibleDeprecationWarning', 'find_common_type', 'isposinf', 'arcsin', 'sctypeNA', 'imag', 'sctype2char', 'singlecomplex', 'SHIFT_DIVIDEBYZERO', 'matrixlib', 'apply_along_axis', 'reciprocal', 'tanh', 'dstack', 'cov', 'cast', 'logspace', 'packbits', 'issctype', 'mgrid', 'longdouble', 'signbit', 'conj', 'asmatrix', 'inf', 'flatiter', 'bitwise_xor', 'fabs', 'generic', 'reshape', 'NaN', 'cross', 'sqrt', '__package__', 'longcomplex', 'complex', 'pad', 'split', 'floor_divide', '__version__', 'format_parser', 'nextafter', 'polyval', 'flipud', 'i0', 'iscomplexobj', 'sys', 'mafromtxt', 'bartlett', 'polydiv', 'stack', 'identity', 'safe_eval', 'greater_equal', 'Tester', 'trapz', 'PINF', 'object', 'recfromtxt', 'oldnumeric', 'add_newdocs', 'RankWarning', 'ascontiguousarray', 'less', 'putmask', 'UFUNC_BUFSIZE_DEFAULT', 'unicode', 'half', 'NAN', 'absolute_import', 'typeDict', '__path__', 'shape', 'setbufsize', 'cfloat', 'RAISE', 'isscalar', 'character', 'bench', 'source', 'add', 'uint16', 'cbrt', 'bool', 'ufunc', 'save', 'ravel', 'float32', 'real', 'int32', 'tril_indices', 'around', 'lexsort', 'complex_', 'ComplexWarning', 'unicode0', 'ipmt', '_import_tools', 'atleast_3d', 'isneginf', 'integer', 'unique', 'mod', 'insert', 'bitwise_not', 'getbufsize', 'array_equiv', 'arange', 'asarray_chkfinite', 'in1d', 'interp', 'hypot', 'logical_and', 'get_printoptions', 'diagflat', 'float128', 'nonzero', 'kaiser', 'ERR_IGNORE', 'polysub', 'fromfile', 'prod', 'nanmax', 'core', 'who', 'seterrobj', 'power', 'bytes_', 'percentile', 'FPE_DIVIDEBYZERO', '__name__', 'subtract', 'print_function', 'nanmean', 'frombuffer', 'iscomplex', 'add_docstring', 'argsort', 'fmin', 'ones_like', 'is_busday', 'arcsinh', 'intc', 'float', 'ndenumerate', 'intp', 'unpackbits', 'Infinity', 'log', 'cdouble', 'complex128', 'long', 'round_', 'broadcast_arrays', 'inner', 'var', 'sctypes', 'log10', 'uintp', 'linalg', 'histogram', 'issubdtype', 'maximum_sctype', 'squeeze', 'int8', 'info', 'seterr', 'argmin', 'genfromtxt', 'maximum', 'record', 'obj2sctype', 'clongdouble', 'euler_gamma', 'arccosh', 'delete', 'tril', 'int', 'ediff1d', 'char', 'single', 'loadtxt', 'hsplit', 'ScalarType', 'triu', 'floating', 'expand_dims', 'floor', 'polyadd', 'nan', 'TooHardError', 'emath', 'arctan', 'bmat', 'isclose', 'ERR_DEFAULT', 'test', 'roll', 'string0', 'compare_chararrays', 'iinfo', 'real_if_close', 'repeat', 'nanvar', 'hamming', 'ALLOW_THREADS', 'ravel_multi_index', 'string_', 'isinf', 'ndarray', 'e', 'ERR_CALL', 'datetime_data', 'clongfloat', 'full_like', 'result_type', 'gradient', 'base_repr', 'argwhere', 'set_string_function']
You can either manually check if a function name you're not sure of appears here using if <function_name> in sys.modules[<module>].__dict__, or you can write a neat automated script that goes through each entry in sys.modules[<module>].
I would favour the latter for anything too sophisticated, and the former for diagnostic purposes.
Rough Implementation of Automatic Tool
A very, very, very quick-and-dirty example of how to write such an automated script:
import re
import sys
with open('file_I_want_to_change.py', 'r+') as f:
file_contents = f.read() # get the entire file as a string
search_string = r"from ([a-zA-Z]+) import *" # regex to find all loaded module names
module_names = re.findall(search_string, file_contents)
map(__import__, module_names) # import ALL of these modules names at once
for module in module_names:
for function_name in sys.modules[module].__dict__:
# do a very quick-and-dirty replace-all
file_contents = file_contents.replace(function_name, "{0}.{1}".format(module, function_name))
f.seek(0) # move to start of file
f.write(file_contents)
This is not very robust, and you shouldn't use it as-is! You may find yourself overwriting names not from the module but that are defined anyway.
It's probably best to allow some form of user interaction to confirm you want to apply a change for each function name found. But it gets the gist across.
This has been tested with the following simple example file:
from numpy import *
array([1])
becomes
from numpy import *
numpy.array([1])
EDIT: I have since created a much more robust and useful command line utility here
Here's a simpler solution. It uses the ast module to strip the code out of a file and then compares it to the list of available functions found by the inspect module. Just replace the yourfilename and the yourmodulename before running.
import ast, inspect, yourmodulename as mymodule
filename='yourfilename.py'
tab = ' '*4
funcs = {m[0] for m in inspect.getmembers(mymodule)
if str(m[1])[1:].split(' ')[0] in ('function', 'class') and
inspect.getmodule(m[1]) == mymodule}
with open(filename) as f:
code = ast.parse(f.read())
words = {node.id for node in ast.walk(code) if isinstance(node, ast.Name)}
print('from', mymodule.__name__, 'import (')
out = tab
for word in (', '.join((sorted(words & funcs)))+')').split():
if len(out + word) > 80:
print(out.rstrip())
out = tab + word + ' '
else:
out += word + ' '
if out:
print(out.rstrip())
Edit: I checked it against my 3k+ line script with dozens of functions and it works.
Edit: Added check to make sure that It's only listing functions that are actually part of the desired module, not its submodules. Also, modified to make it list both classes and functions to import.

Categories