Running entire Python script in one line - python

I have developed a Python script to reference another Python file with functions to compute KPIs. Then, using the main file, the KPIs are cleaned and ordered and finally outputted to a word doc. In the process I am also referring an Excel file to provide further user-desired inputs.
Now I want to be able to just use one line to run the entire file. I have tried and have in the past successfully used:
exec(open("binary2.py").read())
However, with this file, I am running into a certain error which I am not facing when I simply run binary1.py on its own. As in, error happens only using the exec command.
AttributeError Traceback (most recent call last) in
----> 1 exec(open("binary2.py").read())
in
~\Desktop\Model_Validation\Playbook XLS and Jupyter
Notebook\BinaryKPI_PythonFunctionsV3.py in data_vars(df1, target)
662 stack = traceback.extract_stack()
663 filename, lineno, function_name, code = stack[-2]
--> 664 vars_name = re.compile(r'((.?)).$').search(code).groups()[0]
665 final = (re.findall(r"[\w']+", vars_name))[-1]
666
AttributeError: 'NoneType' object has no attribute 'groups'
There is a function which I am calling in the binary2 file which calls for the Traceback. It seems this Traceback is not getting carried over while using exec command.
I also tried:
source = open("binary2.py").read()
code2 = compile(source, filename, 'exec')
exec(code2)
but to no benefit.
Any ideas appreciated.
Thanks,
Ansh
Edit: The code generating an error while running the exec command runs fine while running the py file directly:

Related

Multiprocessing array .get_lock works on one computer but not another

I am working a somewhat extensive python program that uses multiprocessing. Because I wanted the user to see some progress on the console when running the program, I read about using a shared counter on stackoverflow and after a while of playing around with my code, I got it to work. As I said it's too much code to post here, but the gist is that I instantiate a multiprocessing array after the name==main line,
if __name__ == "__main__":
total_progress_counter = Array('i',[0,0])
and then during the main portion of code I pass this array to a function in other module:
some_name.plot(<other variables>,
total_progress_counter=total_progress_counter)
Then within that other function, I used the .get_lock method that I found described here on stackoverflow:
with total_progress_counter.get_lock():
total_progress_counter[0] += self.total_panels_to_plot
I also update the other component, total_progress_counter[1], in the same function. This works fine for me on my work machine, where I wrote the code, and that machine has a Centos operating system.
But, when I run it on my personal MacBook it gives the following traceback:
Traceback (most recent call last):
File "./program.py", line 775, in <module>
program.run()
File "./program.py", line 177, in run
cases_plotted = pool.map(self.__plot__, all_cases)
File "/opt/anaconda3/lib/python3.8/multiprocessing/pool.py", line 364, in map
return self._map_async(func, iterable, mapstar, chunksize).get()
File "/opt/anaconda3/lib/python3.8/multiprocessing/pool.py", line 771, in get
raise self._value
AttributeError: 'list' object has no attribute 'get_lock'
I have python3 version 3.8.3 on my personal machine and python3 version 3.7.4 on my work machine. Can anyone help me understand why I'm getting different behavior on these two environments? I'd be grateful, as this is meant to be software others might use on different machines.

How can I fix AttributeError in Manim Hello World program?

I am attempting to write a Hello World program in Manim.
I have installed Manim and its prerequisite programs, and can run the sample code from the command prompt as intended. This sample code operates in an unusual way; the user issues a command specifying not only a .py file, but also a single class within it, and Python executes the class definition code, seemingly without instantiating the class.
Now I'm trying to write a standalone .py file that works by instantiating a class when run (I am running it in Visual Studio Community 2019), rather than requiring external commands.
I have checked many of the Similar Questions, but unfortunately, they are all about Hello World programs in general, even spanning many non-Python languages.
I found a few AttributeError: '____' object has no attribute '____' questions in search, including this helpful explanation (https://stackoverflow.com/a/8696339/2364796), but nothing that seems to apply to the code I've explicitly written.
I also checked in IRC, and it was suggested that the problem is triggered within the imported code. However, the same code functions properly when imported into the sample, so I must be working with it incorrectly.
This is the current code for my Hello World program.
from manimlib.imports import *
class GreetingScript(Scene):
def construct(self):
characters = TextMobject("Hello World!")
self.add(characters)
scene1 = Scene()
readthrough = GreetingScript(scene1)
This is the error message produced by the above code.
Media will be stored in ./media\. You can change this behavior by writing a
diff
erent directory to media_dir.txt.
Traceback (most recent call last):
File "C:\Users\Admin\Documents\Visual Studio
2019\Projects\PythonApplication1\
PythonApplication1\PythonApplication1.py", line 8, in <module>
scene1 = Scene()
File "C:\Users\Admin\PortableApps\manim-0.1.5\manimlib\scene\scene.py",
line 3
7, in __init__
self, **self.file_writer_config,
File "C:\Users\Admin\PortableApps\manim-
0.1.5\manimlib\scene\scene_file_writer
.py", line 44, in __init__
self.init_output_directories()
File "C:\Users\Admin\PortableApps\manim-
0.1.5\manimlib\scene\scene_file_writer
.py", line 49, in init_output_directories
output_directory = self.output_directory or
self.get_default_output_director
y()
File "C:\Users\Admin\PortableApps\manim-
0.1.5\manimlib\scene\scene_file_writer
.py", line 80, in get_default_output_directory
filename = os.path.basename(self.input_file_path)
AttributeError: 'SceneFileWriter' object has no attribute 'input_file_path'
Press any key to continue . . .
I would expect the output of the program to be a display of the text "Hello World!" but the actual output is AttributeError: 'SceneFileWriter' object has no attribute 'input_file_path' accompanied by the rest of the above message.
The best way to solve this problem is to remove the code that creates the scene1 object. To make this code work, it is required to implement only the source of your scene class, and you can generate the scene using:
$ python -m manim -p /path/to/source.py GreetingScript
The -p flag means to open the video after rendering the scene. I hope this can help on your issue.
from big_ol_pile_of_manim_imports import *
class makeText(Scene):
def construct(self):
#######Code#######
#Making text
first_line = TextMobject("Manim is fun")
second_line = TextMobject("and useful")
final_line = TextMobject("Hope you like it too!", color=BLUE)
color_final_line = TextMobject("Hope you like it too!")
#Coloring
color_final_line.set_color_by_gradient(BLUE,PURPLE)
#Position text
second_line.next_to(first_line, DOWN)
#Showing text
self.wait(1)
self.play(Write(first_line), Write(second_line))
self.wait(1)
self.play(FadeOut(second_line), ReplacementTransform(first_line, final_line))
self.wait(1)
self.play(Transform(final_line, color_final_line))
self.wait(2)
have you tried something?

Accessing a friend TTree using pyROOT

I have a pyROOT script where I use TChain::AddFriend to combine two TTrees:
from ROOT import TFile, gDirectory
myfile = TFile("e.root")
mychain = gDirectory.Get("elec_gen")
entries = mychain.GetEntriesFast()
friendFile = TFile("mu.root")
friendChain = gDirectory.Get("muon_gen")
mychain.AddFriend("muon_gen")
elec_gen_evtNum = mychain.evtNum
muon_gen_evtNum = mychain.muon_gen.evtNum
When I run this I get:
$ python friendTest.py
Traceback (most recent call last):
File "friendTest.py", line 12, in <module>
muon_gen_evtNum = mychain.muon_gen.evtNum
AttributeError: 'TTree' object has no attribute 'muon_gen'
With the last line commented-out it runs fine. So it appears I am not accessing the leaves of the friend tree (muon_gen) correctly. How do I access them?
I also tried combining the TTrees using:
mychain.AddFriend("muon_gen","mu.root")
but this produces the same error.
I had a similar question, and found an answer (well, workaround) on the ROOT forum:
https://root-forum.cern.ch/t/accessing-a-friend-ttree-using-pyroot/25513
although no solution was presented using TFriend.
Instead, RobS found a workaround for his own question of just loading the two TFile and TTree separately and running LoadTree() and GetEvent() on each TChain

matplotlib error when running plotting in multiprocess

I am using python's Multiprocess.Pool to plot some data using multiple processes as follows:
class plotDriver:
def plot(self, parameterList):
numberOfWorkers = len(parameterList)
pool = Pool(numberOfWorkers)
pool.map(plotWorkerFunction, parameterList)
pool.close()
pool.join()
this is a simplified version of my class, the driver also contains other stuffs I choose to omit. The plotWorkderFunction is a single threaded function, which imports matplotlib and does all the plotting and setting figure styles and save the plots to one pdf file, and each worker is not interacting with the other.
I need to call this plot function multiple times since I have many parameterList, like following:
parameters = [parameterList0, parameterList1, ... parameterListn]
for param in parameters:
driver = PlotDriver()
driver.plot(param)
If parameters only contains one parameterList (the for loop only runs once), the code seems working fine. But it consistently fails whenever parameters contains more than one element, with the following error message happening on the second time in the loop.
Traceback (most recent call last):
File "plot.py", line 59, in <module>
plottingDriver.plot(outputFile_handle)
File "/home/yingryic/PlotDriver.py", line 69, in plot
pool.map(plotWrapper, workerParamList)
File "/home/yingryic/.conda/envs/pp/lib/python2.7/multiprocessing/pool.py", line 251, in map
return self.map_async(func.iterable, chunksize).get()
File "/home/yingryic/.conda/envs/pp/python2.7/multiprocessing/pool.py", line 567, in get
raise self._value
RuntimeError: In set_text: could not load glyph
X Error: BadIDChoice (invalid resouce ID chosen for this connection) 14
Extension: 138 (RENDER)
Minor opcode: 17 (RenderCreateGlyphSet)
Resouce id: 0xe00002
: Fatal IO error: client killed
any idea what is going wrong and how should I fix?
You can try placing import matplotlib into plotWorkerFunction() so that child processes will have their own copy of the module.

Can't install trigger network automation tools

I read in the howto documentation to install Trigger, but when I test in python environment, I get the error below:
>>> from trigger.netdevices import NetDevices
>>> nd = NetDevices()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.7/dist-packages/trigger/netdevices/__init__.py", line 913, in __init__
with_acls=with_acls)
File "/usr/local/lib/python2.7/dist-packages/trigger/netdevices/__init__.py", line 767, in __init__
production_only=production_only, with_acls=with_acls)
File "/usr/local/lib/python2.7/dist-packages/trigger/netdevices/__init__.py", line 83, in _populate
# device_data = _munge_source_data(data_source=data_source)
File "/usr/local/lib/python2.7/dist-packages/trigger/netdevices/__init__.py", line 73, in _munge_source_data
# return loader.load_metadata(path, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/trigger/netdevices/loader.py", line 163, in load_metadata
raise RuntimeError('No data loaders succeeded. Tried: %r' % tried)
RuntimeError: No data loaders succeeded. Tried: [<trigger.netdevices.loaders.filesystem.XMLLoader object at 0x7f550a1ed350>, <trigger.netdevices.loaders.filesystem.JSONLoader object at 0x7f550a1ed210>, <trigger.netdevices.loaders.filesystem.SQLiteLoader object at 0x7f550a1ed250>, <trigger.netdevices.loaders.filesystem.CSVLoader object at 0x7f550a1ed290>, <trigger.netdevices.loaders.filesystem.RancidLoader object at 0x7f550a1ed550>]
Does anyone have some idea how to fix it?
The NetDevices constructor is apparently trying to find a "metadata source" that isn't there.
Firstly, you need to define the metadata. Second, your code should handle the exception where none is found.
I'm the lead developer of Trigger. Check out the the doc Working with NetDevices. It is probably what you were missing. We've done some work recently to improve the quality of the setup/install docs, and I hope that this is more clear now!
If you want to get started super quickly, you can feed Trigger a CSV-formatted NetDevices file, like so:
test1-abc.net.example.com,juniper
test2-abc.net.example.com,cisco
Just put that in a file, e.g. /tmp/netdevices.csv and then set the NETDEVICES_SOURCE environment variable:
export NETDEVICES_SOURCE=/tmp/netdevices.csv
And then fire up python and continue on with your examples and you should be good to go!
I found that the default of /etc/trigger/netdevices.xml wasn't listed in the setup instructions. It did indicate to copy from the trigger source folder:
cp conf/netdevices.json /etc/trigger/netdevices.json
But, I didn't see how to specify this instead of the default NETDEVICES_SOURCE on the installation page. But, as soon as I had a file that NETDEVICES_SOURCE pointed to in my /etc/trigger folder, it worked.
I recommend this to get the verifying functionality examples to work right away with minimal fuss:
cp conf/netdevices.xml /etc/trigger/netdevices.xml
Using Ubuntu 14.04 with Python 2.7.3

Categories