I am not able to call one python script from another script using os.system in Pycharm - python-3.x

My sample file in one project is:
import os
feature_name = 'neelam'
combo_boxes = {"'Primary CAN InterfaceComboBox": ["[CanSim, CanSim]", "best_match"],
"'Secondary CAN InterfaceComboBox": ["[CanSim, CanSim]", "best_match"],
"Seed Data SimulationComboBox": ["[Off, OFF]", "best_match"],
"Planter TypeComboBox": ["[_2160_FFT_24R30, 2160 Front Fold 24 Row 30 Inch]", "best_match"],
"BrandComboBox": ["[CaseIH, Case IH]", "best_match"],
"LATAM Gran Fertilizer InstalledComboBox": ["[Hydraulic, Hydraulic]", "best_match"],
"DiagnosticsComboBox": ["[On, ON]", "best_match"]}
for i in combo_boxes:
log_msg = 'Click on combo box {0} at {1}'.format(i, combo_boxes[i][0].split(",")[1][:-1])
print(log_msg)
Now I am trying to run sample file from another python script as given:
import os
script_string = r'C:\Neelam\Neelam_Dev\test_automation\FeatureTestScripts\Common\sample.py'
os.system(script_string)
But after running the second file, it is not printing the output of sample.py. It is just opening that file in Pycharm.

Related

SyntaxError when using print(""" with a lst of numbers to populate a file with GROMACS patched with PLUMED

I am using GROMACS with PLUMED to run MD simulations. In setting up my plumed file for collecting the S2/SH CV from Omar(https://www.plumed.org/doc-v2.8/user-doc/html/_s2_c_m.html) I am having difficulties with the line:
File "makingplumed.py", line 25
""" % (x,i)file=f)
^
SyntaxError: invalid syntax
Here is the code I am trying to run:
# here we create the PLUMED input file with python
with open("plumed.dat","w") as f:
# print initial stuff
#K# from __future__ import print_function
# Define Atoms which are Oxygen hydrogen bond acceptors
ATOMS=[21,35,45,62,76,97,109,133,152,174,188,202,213,227,239,253,269,280,292,311,323,339,353,377,401,416,426,447,466,477,488,503,518,538,560,575,597,617,624,641,655,677,692,702,722,743,765,784,798,820,844,866,883,897,919,939,961,978,988,1004,1021,1040]
#Define heavy atoms for S2CM CV (protein and backbone and not hydrogen)
heavy_atoms_nh: GROUP ATOMS=1,5,7,10,12,16,20,21,22,23,26,29,32,34,35,36,38,40,44,45,46,48,50,53,54,55,57,59,61,62,63,64,67,70,73,75,76,79,81,84,85,87,89,90,92,94,96,97,98,100,102,105,106,107,108,109,110,112,114,117,120,123,124,125,126,129,132,133,134,136,138,141,143,147,151,152,153,155,157,160,163,166,169,173,174,175,177,179,181,185,187,188,189,191,193,195,199,201,202,203,205,207,210,212,213,214,216,217,218,220,224,226,227,228,230,232,235,236,237,238,239,240,241,244,247,250,252,253,254,256,258,260,264,268,269,270,272,274,277,279,280,281,283,285,288,291,392,293,295,297,299,303,306,310,311,312,314,316,319,320,321,322,323,326,328,330,334,338,339,340,342,344,346,350,352,353,354,356,358,361,364,367,369,370,373,376,377,378,380,382,385,388,391,393,397,400,401,402,404,406,409,412,415,416,417,419,421,425,426,427,429,431,434,345,437,439,442,444,446,447,448,450,452,455,457,461,465,466,467,469,471,474,476,477,478,480,482,485,487,489,491,493,496,499,500,501,502,503,504,506,508,511,514,515,516,517,518,519,521,523,526,527,529,531,533,535,537,538,539,541,543,546,549,552,555,559,560,561,563,265,268,571,572,573,574,575,576,578,580,583,586,589,592,596,597,598,600,602,605,606,608,610,612,614,616,617,618,620,623,624,625,627,629,632,635,636,640,641,642,644,646,648,652,654,655,656,658,660,663,666,669,672,676,677,678,680,682,685,688,691,692,693,695,697,701,702,703,705,707,710,711,713,715,717,719,721,722,723,725,727,730,731,733,735,738,740,742,743,744,746,748,751,754,757,760,764,765,766,768,770,773,775,779,783,784,785,786,789,792,795,797,798,799,801,803,806,809,812,815,819,820,821,823,825,828,829,831,833,834,836,838,840,842,843,844,845,847,849,852,855,858,861,865,866,867,869,871,874,877,878,879,882,883,884,886,888,891,892,893,896,867,898,900,902,905,908,911,914,918,919,920,922,924,927,928,930,932,934,936,938,939,940,942,944,947,950,953,956,960,961,962,964,966,969,975,973,977,978,979,981,983,987,988,989,991,993,995,999,1003,1004,1005,1007,1009,1012,1015,1016,1017,1020,1021,1022,1024,1026,1029,1031,1035,1039,1040,1041,1043,1045,1048,1049,1051,1053,1055,1057,1059,1060,1061
for x in range(len(ATOMS)):
for i in range(1, 60):
print("""
S2CM ...
NH_ATOMS=x,x+2
HEAVY_ATOMS=heavy_atoms_nh
LABEL=S2nh-%d
R_EFF=0.10
PREFACTOR_A=0.80
EXPONENT_B=1.0
OFFSET_C=0.10
N_I=1
NOPBC
... S2CM
""" % (x,i)file=f)
I am just learning python and Linux this summer as I am getting involved with computational biochemistry research, so if there is a simple fix I am very sorry for the waste of time, and I appreciate any and all time and attention to this matter.
python3 --version
Python 3.6.13
Thank You,
David Cummins
Masters Student at Western Washington University

Cron doesnt output anything from python script if selenium is run headless

I've got a WEIRD problem, I've been banging my head against this issue for the last 2 weeks, googling around and drinking to wash away the pain of my failures.
So here's the deal, when I run my alarm script from cron, nothing at all is redirected to my log file, and the script doesn't seem to function. HOWEVER, when I comment out a seemingly meaningless option in selenium, the logging works. The script functions perfectly when run outside of cron. I'm using non-sudo cron by the way, as that's where my python environment is set up.
The script basically gets some information and types it into a very nice TTS engine (natural reader) and plays it in the morning. I don't want to pay for a TTS or natural reader which is why I'm using selenium
CODE
Here is my cron script, I've ommited my other cron entries as they are not important:
#reboot sh scripts/YakScripts.sh -alarm > scripts/logs/alarm.log 2>&1
Here is my YakScripts.sh file, again, I've omitted the meat of it:
TZ=":America/New_York" date
export PYTHONPATH=${PYTHONPATH}:"scripts"
case "$1" in
-alarm) python3 scripts/Alarm/alarm.py ;;
*) echo "Thats not a function, dumbass."
;;
esac
Here is my alarm script:
import Alarm.Weather.getDailyForecast as weather
import Alarm.SunCycles.suncycles as suncycles
import TTS.speak as voice
from datetime import date
today = date.today()
# dd/mm/YY
naturalDate = today.strftime("%B %d, %Y")
print(naturalDate)
dayOfWeek = today.strftime("%A")
print(dayOfWeek)
alarmText = "Good Morning. \n"
alarmText += "Today is " + dayOfWeek + ". " + naturalDate + ".\n"
alarmText += weather.getMostRecentForecast() + "\n\n"
alarmText += "Today, the sun rises at " + suncycles.getSunrise() + ".\n"
alarmText += "and sets at " + suncycles.getSunset() + ".\n"
print(alarmText)
voice.speak(alarmText)
And lastly (and the most important bit) here is my speak script, I've removed most of this as I dont think naturalReader would like me posting this. But I tested and it functions the same:
import time
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
import TTS.locators as locators
import globalData
selectedVoice = globalData.tts["voice"]
max_stale_tries = globalData.tts["max_retries"]
def speak(message):
options = Options()
#options.add_argument("--headless")
driver = webdriver.Firefox(options=options)
driver.get("https://www.naturalreaders.com/online/")
print("Loaded the web page")
time.sleep(3)
ISSUES
So with that, here's the problem, when I don't comment out the headless line in there (line 15 of speak.py) this is what my alarm.log file looks like:
Tue Feb 9 21:08:03 EST 2021
and when I comment out that line, this is what It looks like:
Tue Feb 9 21:27:43 EST 2021
February 10, 2021
Wednesday
Good Morning.
Today is Wednesday. February 10, 2021.
Tonight the forecast is Snow showers before 10pm, then snow showers and patchy freezing drizzle. Cloudy, with a low around 8. Wind chill values as low as -6. Northeast wind 8 to 12 mph.
Wednesday the forecast is Snow showers before 7am. Cloudy, with a high near 16. Wind chill values as low as -7. Northeast wind 10 to 14 mph, with gusts as high as 18 mph.
Today, the sun rises at 07:29 AM.
and sets at 06:12 PM.
Traceback (most recent call last):
File "../../../../../../../../scripts/Alarm/alarm.py", line 35, in <module>
voice.speak(alarmText)
File "/scripts/TTS/speak.py", line 16, in speak
driver = webdriver.Firefox(options=options)
File "/home/appa/.local/lib/python3.8/site-packages/selenium/webdriver/firefox/webdriver.py", line 170, in __init__
RemoteWebDriver.__init__(
File "/home/appa/.local/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 157, in __init__
self.start_session(capabilities, browser_profile)
File "/home/appa/.local/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 252, in start_session
response = self.execute(Command.NEW_SESSION, parameters)
File "/home/appa/.local/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "/home/appa/.local/lib/python3.8/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: Process unexpectedly closed with status 1
I know this error has to do with the webdriver not being able to connect to the display, which is why I need to be running this headless.
I'm really just wondering why in the world I get output when that line is uncommented, and DONT get output when run normally (headless).

Python 3: time.tzset() alternative for Windows?

I am new to Python. I am reading about dates and times from the lovely book 'Python 3 Standard Library by Example' by Doug Hellmann and I stumbled upon this code snippet:
import time
import os
def show_zone_info():
print(f'''\
TZ : {os.environ.get('TZ', '(not set)')}
tzname: {time.tzname}
Zone : {time.timezone} ({time.timezone / 3600})
DST : {time.daylight}
Time : {time.ctime()}
''')
if __name__ == '__main__':
print('Default: ')
show_zone_info()
ZONES = [
'GMT',
'Europe/Amsterdam'
]
for zone in ZONES:
os.environ['TZ'] = zone
# time.tzset() # Only available on Unix
print(zone, ':')
show_zone_info()
Problem is, time.tzset() is only available on Unix and without it on Windows machine, timezone doesn't change during the run time of the code. What is the alternative to time.tzset() on Windows? (I am running Python 3.8.3 on Windows 10 at the time of asking this question.)

How do I change the line endings used by PExpect output

The returned output from pexpect.run() includes \r\n at the end of every line. Printing to the terminal using print(returnVal.decode()) correctly prints one line for each line returned. When I examine the output I see that the byte string contains \r\n. When I log that to a file I get double returns to the log file. I'm on a Mac using Python 3.7. Is there a way to set the preferred new line when writing the output? I am using pythons logging class and using the info() method to write the string. Output looks like this:
total 80
-rw-r--r-- 1 xxxx admin 1048 Nov 12 00:41 Constants.py
-rw-r--r-- 1 xxxx admin 5830 Nov 12 13:33 file1.py
-rw-r--r-- 1 xxxx admin 2255 Nov 12 00:51 file2.py
When it should look like:
total 80
-rw-r--r-- 1 xxxx admin 1048 Nov 12 00:41 Constants.py
-rw-r--r-- 1 xxxx admin 5830 Nov 12 13:33 file1.py
-rw-r--r-- 1 xxxx admin 2255 Nov 12 00:51 file2.py
Here is a simplified version of my original Logger class:
class Logger():
def __init__( self, path ):
msgFormat = '%(asctime)s.%(msecs)d\t%(message)s'
dateFormat = '%m/%d/%Y %H:%M:%S'
logging.basicConfig( format=msgFormat, datefmt=dateFormat, filename=path, level=logging.INFO )
def Log ( self, theStr ):
logging.info( str( theStr ))
The string being returned from Pexpect looks something like:
Line1\r\nLine2
Depending on how you log the output, it's advisable to format the newlines before sending to logger. However, if you must override the logging module's newline parameter for FileHandler, and as an experiment, you can do so by monkey patching its _open method as the functionality isn't available by default.
I used source code for Python version 3.8 to get _open function's definition.
import logging
def custom_open(self):
"""
Monkey patched _open function of class logging.FileHandler (Python 3.8)
"""
return open(self.baseFilename, self.mode, encoding=self.encoding, newline='')
logging.FileHandler._open = custom_open
if __name__ == "__main__":
pexpect_return = "Output\nTest"
my_log = logging.getLogger("test_logger")
my_log.setLevel(logging.INFO)
my_log.addHandler(logging.FileHandler("test.log"))
my_log.info(pexpect_return)
How it works
Python's logging module has a class FileHandler, which uses a method _open to create a file handler object to write and append to log files on disk. Its default implementation as of version 3.8 does not have the newline parameter so it uses default newlines.
Monkey patching is when you replace or update a method/function in one of your imported classes, as the program is running. This line logging.FileHandler._open = custom_open tells python to replace the _open method of the FileHandler class, with my custom_open method. Then later when I use my_log.addHandler(logging.FileHandler("test.log")), the new custom_open method is used to open the file with newline paramater.
You can further confirm that the new method is used to open the file by adding a suffix to the file name like this:
return open(self.baseFilename+"__Monkey_Patched", self.mode, encoding=self.encoding, newline='')
If you will now run that demo code, the filename will be "test.log__Monkey_Patched".
This code, however, will not replace any newline characters which you pass to the logger as part of the string to log. You need to process that beforehand.

How to profile a vim plugin written in python

Vim offers the :profile command, which is really handy. But it is limited to Vim script -- when it comes to plugins implemented in python it isn't that helpful.
Currently I'm trying to understand what is causing a large delay on Denite. As it doesn't happen in vanilla Vim, but only on some specific conditions which I'm not sure how to reproduce, I couldn't find which setting/plugin is interfering.
So I turned to profiling, and this is what I got from :profile:
FUNCTION denite#vim#_start()
Defined: ~/.vim/bundle/denite.nvim/autoload/denite/vim.vim line 33
Called 1 time
Total time: 5.343388
Self time: 4.571928
count total (s) self (s)
1 0.000006 python3 << EOF
def _temporary_scope():
nvim = denite.rplugin.Neovim(vim)
try:
buffer_name = nvim.eval('a:context')['buffer_name']
if nvim.eval('a:context')['buffer_name'] not in denite__uis:
denite__uis[buffer_name] = denite.ui.default.Default(nvim)
denite__uis[buffer_name].start(
denite.rplugin.reform_bytes(nvim.eval('a:sources')),
denite.rplugin.reform_bytes(nvim.eval('a:context')),
)
except Exception as e:
import traceback
for line in traceback.format_exc().splitlines():
denite.util.error(nvim, line)
denite.util.error(nvim, 'Please execute :messages command.')
_temporary_scope()
if _temporary_scope in dir():
del _temporary_scope
EOF
1 0.000017 return []
(...)
FUNCTIONS SORTED ON TOTAL TIME
count total (s) self (s) function
1 5.446612 0.010563 denite#helper#call_denite()
1 5.396337 0.000189 denite#start()
1 5.396148 0.000195 <SNR>237_start()
1 5.343388 4.571928 denite#vim#_start()
(...)
I tried to use the python profiler directly by wrapping the main line:
import cProfile
cProfile.run(_temporary_scope(), '/path/to/log/file')
, but no luck -- just a bunch of errors from cProfile. Perhaps it is because the way python is started from Vim, as it is hinted here that it only works on the main thread.
I guess there should be an easier way of doing this.
The python profiler does work by enclosing the whole code,
cProfile.run("""
(...)
""", '/path/to/log/file')
, but it is not that helpful. Maybe it is all that is possible.

Resources