Python printing a string yields an unexpected result - python

Trying to print / work with a specific String is driving me crazy in Python - or to specify this: I am using Jython.
The simple command
print "appilog.xxxxx.xxxxx.xxxxxxx"
results in a print of something looking like a java package
com.xxxxx.xxxxx.xxxxxx
Does Python/Jython do any special lookup for strings? Is there a way to enforce the usage of the "original" string I entered before?
Other things I tried are the following:
print ("appilog...")
print r"appilog..."
print str("appilog...")
print str(r"appilog...")
Imports used in the script this command is located in are the following:
from com.hp.ucmdb.discovery.probe.services.dynamic.core import EnvironmentInformation
#coding=utf-8
import string
import re
import sys
import os
import ConfigParser
import shutil
import StringIO
import logger
import modeling
import time
import subprocess
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import javax.management.MBeanServerConnection;
import javax.management.ObjectName;
import javax.management.openmbean.CompositeDataSupport;
import javax.management.openmbean.CompositeType;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;
import datetime
from appilog.common.system.types.vectors import ObjectStateHolderVector

Related

Python Automation using OTA for Hp Alm

How to get projects and no of test cases,status in the AlM using python OTA?
import win32com.client
from re import split
from win32ui import IsObject
from test.test_pydoc import TestDescriptions
import os
def execute_testlab(self,ALM_TEST_SET_PATH):
bloomtree=self.TDConnection.TreeManager
try:
tsFolder=self.TDConnection.TestSetTreeManager.NodeByPath(ALM_TEST_SET_PATH)
print(tsFolder)
tsFactory=tsFolder.TestSetFactory
td_tsetfilter=tsFactory.Filter
td_testset=td_tsetfilter.NewList()
for singletest in td_testset:
print(singletest.Name)
td_tsTestSetFactory=singletest.TSTestFactory
td_testlist=td_tsTestSetFactory.NewList("")
for testitem in td_testlist:
print("testname:",testitem.Name)
lclRunFactory=testitem.RunFactory
lclobjRun=lclRunFactory.AddItem(testitem,"No Run")
lclobjRun.StartExecution()
lclobjRun.Post()
lclobjRun.Run()

Is there a way to manage huge many import statements in Python?

I am working on a Flask-RESTFul API where I have so many import statements in my app.py file as below.
from flask import Flask
from flask_restful import Api
from apis.ChambAvail import ChambAvail
from apis.ChambAvailBal import ChambAvailBal
from apis.ChambTwInhibit import ChambTwInhibit
from apis.CurrentWip import CurrentWip
from apis.Detail import Detail
from apis.IncomingWip import IncomingWip
from apis.Info import Info
from apis.Manage import Manage
from apis.MfCount import MfCount
from apis.OtherHold import OtherHold
from apis.PqeDue import PqeDue
from apis.QualInhibit import QualInhibit
from apis.ReleaseBef import ReleaseBef
from apis.RfInhibit import RfInhibit
from apis.SketchDetailedLot import SketchDetailedLot
from apis.SketchLotDetail import SketchLotDetail
from apis.SketchMainDetail import SketchMainDetail
from apis.SketchMainRC import SketchMainRC
from apis.SketchDesignInfo import SketchDesignInfo
from apis.SketchOneDetail import SketchOneDetail
from apis.SketchOneLotDetail import SketchOneLotDetail
from apis.SketchOneMain import SketchOneMain
from apis.SketchOneNearestLot import SketchOneNearestLot
from apis.SketchTimeList import SketchTimeList
from apis.SketchWsgList import SketchWsgList
from apis.TakeEquipDetail import TakeEquipDetail
from apis.TakeEquipLotDetail import TakeEquipLotDetail
from apis.TakeEquipMain import TakeEquipMain
from apis.TakeOneDetail import TakeOneDetail
from apis.TakeOneEquipData import TakeOneEquipData
from apis.TakeOneMain import TakeOneMain
from apis.TakeOneRADetail import TakeOneRADetail
from apis.UpdBufInputCapacity import UpdBufInputCapacity
from apis.UpdEohInputCapacity import UpdEohInputCapacity
from dbmanager import datasource
from logmanager.setlogger import logger
from utils import config_reader
env = 'test'
app = Flask(__name__)
api = Api(app)
api.add_resource(SketchMainRC, '/SketchMainrc')
api.add_resource(SketchDesignInfo, '/SketchDesigninfo')
api.add_resource(SketchWsgList, '/Sketchwsglist')
api.add_resource(SketchMainDetail, '/Sketchmaindetail')
api.add_resource(SketchLotDetail, '/Sketchlotdetail')
api.add_resource(SketchOneMain, '/SketchOneMain')
api.add_resource(SketchOneDetail, '/SketchOnedetail')
api.add_resource(SketchOneLotDetail, '/SketchOnelotdetail')
api.add_resource(SketchOneNearestLot, '/SketchOnenearestlot')
api.add_resource(SketchDetailedLot, '/Sketchdetailedlot')
api.add_resource(SketchTimeList, '/Sketchtimelist')
api.add_resource(TakeEquipMain, '/TakeequipMain')
api.add_resource(TakeEquipDetail, '/Takeequipdetail')
api.add_resource(TakeEquipLotDetail, '/Takeequiplotdetail')
api.add_resource(TakeOneMain, '/TakeOneMain')
api.add_resource(TakeOneDetail, '/TakeOnedetail')
api.add_resource(TakeOneRADetail, '/TakeOneradetail')
api.add_resource(TakeOneEquipData, '/TakeOneequipData')
api.add_resource(ChambAvail, '/chambavail')
api.add_resource(ChambAvailBal, '/chambavailbal')
api.add_resource(ChambTwInhibit, '/chambtwinhibit')
api.add_resource(CurrentWip, '/currentwip')
api.add_resource(IncomingWip, '/incomingwip')
api.add_resource(Detail, '/detail')
api.add_resource(Manage, '/manage')
api.add_resource(MfCount, '/mfcount')
api.add_resource(OtherHold, '/otherhold')
api.add_resource(PqeDue, '/pqedue')
api.add_resource(QualInhibit, '/qualinhibit')
api.add_resource(ReleaseBef, '/releasebef')
api.add_resource(RfInhibit, '/rfinhibit')
api.add_resource(Info, '/info')
api.add_resource(InputCapacity, '/inputcapacity')
api.add_resource(InputCapacity, '/inputcapacity')
app.ls_sf_db_env = 'test'
app.ls_config = config_reader.get_config('test')
# Initialise the connection pool
datasource.initial_all_fab_engine(app)
if __name__ == '__main__':
logger.info('Starting the server')
app.run(port=5000, debug=True)
Below is my project structure.
These are just some of the APIs I completed & there are 100 more. I am new to Python, particularly to Flask-RESTFul API Development. I have separate classes for each API, I thought code looks cleaner that way. But in my app.py file, these import statements are piling up. Is there a better way to add/manage these import statements & also the add.resource() statements in a better way in my app.py file ? Could anyone let me know if there is a way I can import all these statements differently & keep my app.py cleaner ?
Any help is appreciated.
I'd do some path-based magic.
The following snippet will list all .py files in the apis/ subdir, import them as modules, extract a class that is named same as the file, and register it under /ClassName endpoint.
from importlib import import_module
from pathlib import Path
HERE = Path(__file__).parent
for api_class_file in HERE.glob("apis/*.py"):
# apis/ChambAvail.py -> ChambAvail
api_class_name = api_class_file.stem
if api_class_name.startswith("__"):
# skip special files such as __init__ and __main__
continue
# equivalent to "import apis.ChambAvail"
api_module = import_module(f"apis.{api_class_name}")
# equivalent to "apis.ChambAvail.ChambAvail
api_class = getattr(api_module, api_class_name)
api.add_resource(api_class, f"/{api_class_name}")

Print version of a module without importing the entire package

Is it possible to check the version of a package if only a module is imported?
When a package is imported like...
import pandas as pd
I use:
print('pandas : version {}'.format(pd.__version__))
to print the version number.
How do I check the version number if only a module is imported, like
import matplotlib.pyplot as plt
or
from sklearn.metrics import confusion_matrix
Any suggestions?
I usually do this:
import matplotlib.pyplot as plt
import sys
print (sys.modules[plt.__package__].__version__)
if you import just a function:
from sklearn.metrics import confusion_matrix as function
import sys
try:module_name = function.__module__[:function.__module__.index(".")]
except:module_name = function.__module__
print (sys.modules[module_name].__version__)
and if this doesn't work you could just import pip and for loop all the modules.

how to call a module in a function in pp where that fuction has other functions in it?

I'm currently using parallel python ,and in the parameters of job_server.submit i added the library in modules but the problem is that even that library has other librairies in it .so what should i do ?
Here is the code i'm trying to run:
from tools.demo import detect_cn
import pp
job_server = pp.Server()
f1 = job_server.submit(detect_cn, (filename,),modules=('tools.demo',))
f2 = job_server.submit(detect_cn, (filename1,),modules=('tools.demo',))
cnis, preproc_time, roi_file_images=f1()
cnis1, preproc_time1, roi_file_images1=f2()
and this is part of code of demo.py
import _init_paths
from fast_rcnn.config import cfg
from fast_rcnn.test import im_detect
from fast_rcnn.nms_wrapper import nms
from utils.timer import Timer
from ocr.clstm import clstm_ocr
from ocr.clstm import clstm_ocr_calib
import matplotlib.pyplot as plt
import numpy as np
import scipy.io as sio
import caffe, os, sys, cv2
import argparse
import werkzeug
import datetime
import math
import pytesseract
from PIL import Image
def detect_cn(filename):
cfg.TEST.HAS_RPN = True # Use RPN for proposals
args = parse_args()
prototxt = os.path.join(cfg.MODELS_DIR, NETS[args.demo_net][0],
'faster_rcnn_alt_opt', 'faster_rcnn_test.pt')
caffemodel = os.path.join(cfg.DATA_DIR, 'faster_rcnn_models',
NETS[args.demo_net][1])
if not os.path.isfile(caffemodel):
raise IOError(('{:s} not found.\nDid you run ./data/script/'
'fetch_faster_rcnn_models.sh?').format(caffemodel))
if args.cpu_mode:
caffe.set_mode_cpu()
else:
caffe.set_mode_gpu()
caffe.set_device(args.gpu_id)
cfg.GPU_ID = args.gpu_id
net = caffe.Net(prototxt, caffemodel, caffe.TEST)
print '\n\nLoaded network {:s}'.format(caffemodel)
print '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~'
print 'Demo for CN image...'
return demo2(net, filename)
Do you think i should load all those librairies in modules of job server.submit?
I want to use the pp bacause detect_cn takes 2 minutes to give results
any ideas?
Yes, you should import all these modules when wou submit your function into the execution queue.
f1 = job_server.submit(detect_cn, (filename,),modules=("math","numpy", ...))

do I have a circular import in python

I'm trying to port over a python project from v2.x, to v3.x
one of the major changes to python was the import system.
I am now seeing an error when trying to load my python notebook as follows
package/
__init__.py
bh_tsne.py
Collect Samples.ipynb //imports utils.list_all_files, sees error
Error Output
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-2-1339232cd15c> in <module>()
1 import numpy as np
2 from os.path import join
----> 3 from utils.list_all_files import list_all_files
4 from multiprocessing import Pool
/~/AudioNotebooks/utils/__init__.py in <module>()
4 from . import show_array
5 from . import make_mosaic
----> 6 from . import bh_tsne
7 from . import normalize
8 from . import mkdir_p
ImportError: cannot import name 'bh_tsne'
strangely.. I think the problem is a circular dependence.. but bh_tsne doesn't rely on any utilities.. could the circularity be coming from my utils.list_all_files and then the __init__.py ?
bh_tsne imports
from argparse import ArgumentParser, FileType
from os.path import abspath, dirname, isfile, join as path_join
from shutil import rmtree
from struct import calcsize, pack, unpack
from subprocess import Popen
from sys import stderr, stdin, stdout
from tempfile import mkdtemp
from platform import system
from os import devnull
import numpy as np
import os, sys
import io
Edit
Is that redundant os.path join perhaps the root cause?
I ended up just upgrading the wrapper that was used in one project from it's source upstream project. The original owner had done upgrades.
https://github.com/lvdmaaten/bhtsne/blob/master/bhtsne.py
and the import worked after that as
import utils.bhtsne as bhtsne
I found out that bh_tsne seems NOT to work with python 3. Also another version (Multicore TSNE) just work with python 2.7 as well

Categories