I want to do a script in ANSA for importing a LS DYNA deck. I am only getting the output message "Code generation completed" but it doesn't import anything. Here is the line I am using:
PYTHON script
import os
import ansa
from ansa import base
from ansa import constants
from ansa import utils
def main():
deck=constants.LSDYNA
base.InputLSDyna(filename="C:/Documents/Model_test.k", nodes_id="offset", elements_id="offset", properties_id="offset", materials_id="offset", sets_id="offset", model_action="new_model_in_active_window", version="971r10")
if __name__ == '__main__':
main()
Can anybody help please?
Thank you
Related
I have bunch of python script files that i've created, and running them one by one. how can call these script files and run methods one by one from a different script file. I need little help as method in each file is named main() and i'm not sure how to import and call this method by the same name.
file1.py
import sys
def main():
#do something
if __name__ == '__main__':
main()
file2.py
import sys
def main():
#do something else
if __name__ == '__main__':
main()
You can simply import them as modules, and call the main() function for each
import file1, file2
file1.main()
file2.main()
I think the following syntax will work for you:
from file1 import main as main1
from file2 import main as main2
...
if you want to import main from file1.py into file2.py, you can do from file1 import main as main1 or some other name. This way, you give an alias to the function name. Alternatively, import file1 and then call file1.main()
I pulled code from another team member whose code base works without errors, but anytime I try to run this I get this error. I tried renaming the class and importing in different ways but nothing's been working. Here's the following main code.
import sys
from PySide6.QtCore import *
from PySide6.QtWidgets import *
from PySide6.QtGui import *
from PySide6.QtCharts import *
from database.db import MySql
from package.main_chart import StockChart
from package.indicator_charts import IndicatorCharts
from scripts.custom_timer import RepeatedTimer
here is the current file structure
Why is RepeatedTimer not being detected?
Ok, if it is in custom_timer.py then use
from scripts.custom_timer import RepeatedTimer
You can have a look at https://docs.python.org/3/reference/import.html#submodules
I have been trying to solve this problem for some hours without any success, I have a PHP background and it will work there but not with python
Let's suppose I have a file named Main.py:
import time
import myfunction
myfunction.Calculate()
And myfunction.py is something like:
def Calculate()
print('Thank you')
time.sleep(1)
When I run Main.py, it will crash saying 'time is not defined in my function', but it was defined even before importing my_function, why it does not work?
The simplest solution is to import time in your myfunction.py file. Import things where they are really used:
myfunction.py
import time
def Calculate()
print('Thank you')
time.sleep(1)
main.py
import myfunction
myfunction.Calculate()
I have two python scripts which are in the same directory(C:\\Users\\user1\\general). I want to execute a function in one script from the 2nd second script and so I am trying to import script1 in script2. Here is my script2:
import sys
sys.path.insert(0, 'C:\\Users\\user1\\general')
import script1
from flask import Flask
from flask_socketio import SocketIO, send
app = Flask(__name__)
app.config['SECRET_KEY'] = 'mysecret'
socketio = SocketIO(app)
#socketio.on('message')
def handleMessage(msg):
print('Message: ' + msg)
# script1 has function called api_call(id,message)
messsage = api_call('user1', msg)
send(messsage, broadcast=True)
if __name__ == '__main__':
socketio.run(app)
Here is my script1:
import sys
def api_call(Id,message):
# Code processing
if __name__ == '__main__':
uId = sys.argv[0]
message = sys.argv[1]
api_call(userId,message)
When I execute above script2 I get NameError: name 'api_call' is not defined. It seems somehow the script1 is not getting imported and so the function is not getting through.
Note: Earlier I had tried without using sys.path.insert() and same outcome then also.
Try from script1 import api_call: this would allow to import from script1 module the api_call function.
Python 2
Create __init__.py in the same folder.
Import using following command
from script1 import api_call
Python 3
Import using following command
from .script1 import api_call
I am a newbie in process of learning python and currently working on a automation project.
And i have N numbers of testcase which needs to be run on reading material people suggest me to use nosetest.
What is the way to run multiple testcase using nosetest?
And is the correct approach doing it:
import threading
import time
import logging
import GLOBAL
import os
from EPP import EPP
import Queue
import unittest
global EPP_Queue
from test1 import test1
from test2 import test2
logging.basicConfig(level=logging.DEBUG,
format='(%(threadName)-10s) %(message)s',
)
class all_test(threading.Thread,unittest.TestCase):
def cleanup():
if os.path.exists("/dev/epp_dev"):
os.unlink("/dev/epp_dev")
print "starts here"
server_ip ='192.168.10.15'
EppQueue = Queue.Queue(1)
EPP = threading.Thread(name='EPP', target=EPP,
args=('192.168.10.125',54321,'/dev/ttyS17',
EppQueue,))
EPP.setDaemon(True)
EPP.start()
time.sleep(5)
suite1 = unittest.TestLoader().loadTestsFromTestCase(test1)
suite2 = unittest.TestLoader().loadTestsFromTestCase(test2)
return unittest.TestSuite([suite1, suite2])
print "final"
raw_input("keyy")
def main():
unittest.main()
if __name__ == '__main__':
main()
Read
http://ivory.idyll.org/articles/nose-intro.html.
Download the package
http://darcs.idyll.org/~t/projects/nose-demo.tar.gz
Follow the instructions provided in the first link.
nosetest, when run from command line like 'nosetest' or 'nosetest-2.6' will recursively hunt for tests in the directory you execute it in.
So if you have a directory holding N tests, just execute it in that directory. They will all be executed.