SyntaxWarning: import * only allowed at module level - python

I am trying to make someone else program work. I have no experience of Python. I would appreciate if someone could help me here. I get the following error with python 2.6:
WSHSP.py:598: SyntaxWarning: import * only allowed at module level
def drawComposition(self, solution, goalService):
WSHSP.py:598: SyntaxWarning: import * only allowed at module level
def drawComposition(self, solution, goalService):
C:\WSPR\WebServicePath.py:3: DeprecationWarning: the sets module is deprecated
from sets import Set
here is the code:
def SMxmlPrint(self, solution, goalService, node_case):
parent = node_case
OPEN = []
CLOSE =[]
OPEN = solution
itr = 1
state = set(goalService.inputList)
for t in goalService.inputList:
if self.typeTable.has_key(t):
state |= set(self.typeTable[t])
while True:
for ws in OPEN:
if set(self.webServiceList[ws].inputList).issubset(state):
CLOSE.append(ws)
parent = self.appendChildNode(parent, str(itr), ws)
itr +=1
for ws in CLOSE:
state = state.union(self.webServiceList[ws].outputList)
for t in self.webServiceList[ws].outputList:
if self.typeTable.has_key(t):
state |= set(self.typeTable[t])
OPEN = list ( Set(solution).difference(Set(CLOSE)) )
if len(OPEN) is 0:
break
def drawComposition(self, solution, goalService):
try:
from pylab import *
except:
print ("pylab not found: see https://networkx.lanl.gov/Drawing.html for info")
raise
from networkx import *

I don't think it's a good idea to ignore warnings, but if you simply must get it out of your sight, you can use the -W flag on the command line:
python -W ignore your_script_name.py

Related

Call python function with arguments and get returned value in autohotkey

I have a python script called "server.py" and inside it I have a function def calcFunction(arg1): ... return output How can I call the function calcFunction with arguments and use the return value in autohotkey? This is what I want to do in autohotkey:
ToSend = someString ; a string
output = Run server.py, calcFunction(ToSend) ; get the returned value from the function with ToSend as argument
Send, output ; use the returned value in autohotkey
I have looked online but nothing seems to fully answer my question. Can it even be done?
In order to send your parameters to Python, you could use arguments from within your Python script. You can do this with the sys library:
import sys
print(sys.argv[0]) # name of file
print(sys.argv[1]) # first argument
print(sys.argv[2]) # second argument...
From within your AutoHotKey script, you can send parameters to the Python script by adding them as arguments right after specifying the file name:
RunWait, server.py "This will be printed as the first argument!" "This is the second!"
Then, to get the output of the function back to AHK, you could use sys again by utilizing it's exit() function:
sys.exit(EXIT_NUMBER)
And back in AHK, you recieve the EXIT_NUMBER inside the variable ErrorLevel.
Put all together, your code should look something like this:
; AHK
RunWait, server.py "%ToSend%"
# Python
sys.exit(calcFunction(sys.argv[1]))
; AHK
MsgBox %ErrorLevel%
using python COM server, ahk can really calls python functions. directly.
you use it like this: MsgBox % pythonComServer.toUppercase("hello world")
simple example: return uppercased string
use the python part from How to program hotstrings in python like in autohotkey and use this for ahk part:
call python function uppercase.ahk
#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
#SingleInstance, force
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.
SetBatchLines, -1
#KeyHistory 0
ListLines Off
pythonComServer:=ComObjCreate("Python.stringUppercaser")
;or
; pythonComServer:=ComObjCreate("{C70F3BF7-2947-4F87-B31E-9F5B8B13D24F}") ;use your own CLSID
MsgBox % pythonComServer.toUppercase("hello world")
Exitapp
f3::Exitapp
customized version: (math) use SymPy to simplify Expression
read this first to understand: How to program hotstrings in python like in autohotkey
sympy com server.py
from sympy import simplify, Number, N
from sympy.parsing.sympy_parser import standard_transformations, implicit_multiplication_application, convert_xor
from sympy.parsing.sympy_parser import parse_expr
from decimal import Decimal
from winsound import MessageBeep
transformations = standard_transformations + (implicit_multiplication_application, convert_xor)
def removeTrailingZerosFromNum(num):
dec = Decimal(str(num))
tup = dec.as_tuple()
delta = len(tup.digits) + tup.exponent
digits = ''.join(str(d) for d in tup.digits)
if delta <= 0:
zeros = abs(tup.exponent) - len(tup.digits)
val = '0.' + ('0' * zeros) + digits
else:
val = digits[:delta] + ('0' * tup.exponent) + '.' + digits[delta:]
val = val.rstrip('0')
if val[-1] == '.':
val = val[:-1]
if tup.sign:
return '-' + val
return val
def removeTrailingZerosFromExpr(operatorObject):
if operatorObject.args:
return type(operatorObject)(*[removeTrailingZerosFromExpr(i) for i in operatorObject.args])
else:
try:
return Number(removeTrailingZerosFromNum(operatorObject))
except:
return operatorObject
def removeTrailingZerosFromExprOrNumber(operatorObject):
try:
return removeTrailingZerosFromNum(operatorObject)
except:
return removeTrailingZerosFromExpr(operatorObject)
class BasicServer:
# list of all method names exposed to COM
_public_methods_ = ["parExprN"]
#staticmethod
def parExprN(clipBak):
parsed = parse_expr(clipBak, transformations=transformations)
simplified = simplify(N(parsed))
finalStr = str(removeTrailingZerosFromExprOrNumber(simplified))
MessageBeep(-1)
return finalStr.replace("**", "^")
if __name__ == "__main__":
import sys
if len(sys.argv) < 2:
print("Error: need to supply arg (""--register"" or ""--unregister"")")
sys.exit(1)
else:
import win32com.server.register
import win32com.server.exception
# this server's CLSID
# NEVER copy the following ID
# Use "print(pythoncom.CreateGuid())" to make a new one.
myClsid="{4530C817-6C66-46C8-8FB0-E606970A8DF6}"
# this server's (user-friendly) program ID, can be anything you want
myProgID="Python.SimplifyExpr"
import ctypes
def make_sure_is_admin():
try:
if ctypes.windll.shell32.IsUserAnAdmin():
return
except:
pass
exit("YOU MUST RUN THIS AS ADMIN")
if sys.argv[1] == "--register":
make_sure_is_admin()
import pythoncom
import os.path
realPath = os.path.realpath(__file__)
dirName = os.path.dirname(realPath)
nameOfThisFile = os.path.basename(realPath)
nameNoExt = os.path.splitext(nameOfThisFile)[0]
# stuff will be written here
# HKEY_LOCAL_MACHINE\SOFTWARE\Classes\CLSID\${myClsid}
# HKEY_LOCAL_MACHINE\SOFTWARE\Classes\CLSID\{c2467d33-71c5-4057-977c-e847c2286882}
# and here
# HKEY_LOCAL_MACHINE\SOFTWARE\Classes\${myProgID}
# HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Python.SimplifyExpr
win32com.server.register.RegisterServer(
clsid=myClsid,
# I guess this is {fileNameNoExt}.{className}
pythonInstString=nameNoExt + ".BasicServer", #sympy com server.BasicServer
progID=myProgID,
# optional description
desc="(math) SymPy simplify Expression",
#we only want the registry key LocalServer32
#we DO NOT WANT InProcServer32: pythoncom39.dll, NO NO NO
clsctx=pythoncom.CLSCTX_LOCAL_SERVER,
#this is needed if this file isn't in PYTHONPATH: it tells regedit which directory this file is located
#this will write HKEY_LOCAL_MACHINE\SOFTWARE\Classes\CLSID\{4530C817-6C66-46C8-8FB0-E606970A8DF6}\PythonCOMPath : dirName
addnPath=dirName,
)
print("Registered COM server.")
# don't use UseCommandLine(), as it will write InProcServer32: pythoncom39.dll
# win32com.server.register.UseCommandLine(BasicServer)
elif sys.argv[1] == "--unregister":
make_sure_is_admin()
print("Starting to unregister...")
win32com.server.register.UnregisterServer(myClsid, myProgID)
print("Unregistered COM server.")
else:
print("Error: arg not recognized")
to register:
python "sympy com server.py" --register
sympy com client.ahk
#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
#SingleInstance, force
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.
SetBatchLines, -1
#KeyHistory 0
ListLines Off
sympyComServer:=ComObjCreate("Python.SimplifyExpr")
;or
; pythonComServer:=ComObjCreate("{4530C817-6C66-46C8-8FB0-E606970A8DF6}") ;use your own CLSID
; clipboard:=sympyComServer.parExprN("1+3*7")
clipboard:=sympyComServer.parExprN("1/3 + 1/2")
$#s::
clipboard:=sympyComServer.parExprN(clipboard)
return
f3::Exitapp

Easy way to determine a type of file (regular file, directory, symlink etc.) by path in python

I wanted an easy way to determine a type of path so I googled alot and then I wrote this:
from stat import S_ISREG, S_ISDIR, S_ISLNK
from os import stat, lstat
from os.path import isfile, islink, isdir, lexists, exists
from enum import Enum, auto
class FileTypes(Enum):
FILE = auto()
LINK_TO_FILE = auto()
DIR = auto()
LINK_TO_DIR = auto()
BROKEN_LINK = auto()
NO_SUCH = auto()
UNDEFINED = auto()
def file_type(filename):
if lexists(filename):
if isfile(filename):
if islink(filename):
return FileTypes.LINK_TO_FILE
else:
return FileTypes.FILE
else:
if isdir(filename):
if islink(filename):
return FileTypes.LINK_TO_DIR
else:
return FileTypes.DIR
else:
if islink(filename):
return FileTypes.BROKEN_LINK
else:
return FileTypes.UNDEFINED
else:
return FileTypes.NO_SUCH
Then I googled more and wrote this:
def file_type2(filename):
if lexists(filename):
if exists(filename):
mode = stat(filename).st_mode
lmode = lstat(filename).st_mode # os.lstat doesn't follow symlinks
if S_ISREG(mode) and S_ISREG(lmode):
return FileTypes.FILE
elif S_ISREG(mode) and S_ISLNK(lmode):
return FileTypes.LINK_TO_FILE
elif S_ISDIR(mode) and S_ISDIR(lmode):
return FileTypes.DIR
elif S_ISDIR(mode) and S_ISLNK(lmode):
return FileTypes.LINK_TO_DIR
else:
return FileTypes.UNDEFINED
else:
return FileTypes.BROKEN_LINK
else:
return FileTypes.NO_SUCH
Both functions do what I want, but look kinda ugly and I think that I'm missing a simpler solution hiding in some cool python lib.
Question is: Is there a better way to do this?
You can try the pathlib module which has been in stdlib since Python 3.4 (for older pythons use pip install pathlib). It defines the Path class which contains methods for both checking types of files as well as resolving symlinks. Besides, it provides a pretty convenient API:
>>> from pathlib import Path
>>> path = Path("/etc/") / "passwd"
>>> path
PosixPath('/etc/passwd')
>>> path.is_file()
True
We can make it more consise with utilizing bitmasking inside enum:
Let's assume first value describes if file exists: 0 for existing and 1 for not existing, second one will be symlink: 1 for links and 0 for non-links, third for directory: 1 if it is directory and 0 if it is not, and the last for file in hte same menner.
So if we wanted to describe file that existsand is a symlink to file, we would use 0(exists)1(link)0(non-dir)1(file)
With using meaningful values we can now consisely chain those values together with results returned from python stat wrapper.
class FileTypes(Enum):
FILE = 1 #0001
LINK_TO_FILE = 5 #0101
DIR = 2 #0010
LINK_TO_DIR = 6 #0110
BROKEN_LINK = 4 #0100
NO_SUCH = 0 #1000
UNDEFINED = #0000
def file_type(filepath):
return FileTypes.NO_SUCH if lexists(filepath) else
Filetypes(int(
str(int(islink(filepath)))
+ str(int(isdir(filepath)))
+ str(int(isfile(filepath)))))
Obviously there is issue of some illegal states like if something would report that it is both directory and file, at that point this will raise exception, it can be modified for different behaviour, but raising exception seems perfectly valid.
Also I've used pretty ugly way of adding together this value, but this is for sake of readibility. You always could do ''.join(map(str,map(int,[islink(filepath),isdir(filepath),isfile(filepath)]))) or even shorter ways

python parallell namspace issue

I have a problem with the namespace in a simple Python Program: can anyone point me in the right direction
import numpy as np
import simple_sim
from IPython.parallel import Client
prescale_steps = np.linspace(0.5, 1.5, 101)
val = []
c = Client()
dview = c[:]
dview.execute('import simple_sim')
dview.execute('from numpy import *')
dview['prescale_steps'] = prescale_steps
dview['val'] = val
detuning_steps = np.linspace(-11,11,101)
def fid(det):
for p in prescale_steps:
tlist, ret = simple_sim.simple_simulation(pulse_file='/home/andreas/Dropbox/puls25p8gn15map.mat', pulse_length=0.5, gamma=0, detuning=det, prescale=p)
val.append(np.array([d,p,ret[-1]]))
return val
lview = c.load_balanced_view()
res = lview.map(fid, detuning_steps)
a = res.get()
a = np.asarray(a)
always raises the Error: global name 'simple_sim' is not defined, although it should be defined shouldn't it?
Make sure that simple_sim is in the path for your ipython engines, not just your ipython shell.
I.e. if simple_sim.py in ~/mydir/, you need to run ipcluster start --n=4 in ~/mydir/ or have ~/mydir in your $PYTHON_PATH for the shell running ipcluster.
Not 100% sure, but it could be that simple_sim isin't in the site packages or in the same folder as dview. In the other words dview cant find your simple_sim module and therefore it produces error. However if that happens not to be case, I'm not sure what produces that error.

Vim python#complete doesn't work for "from .module import" statement

Current python#complete doesn't support any python script with the following import statement:
from . import module
from .modulea import abc
It will show "from: syntax error..." in vim.
Anyone has any clue to solve it?
I spend some time today just to resolve this issue myself by going through the pythoncomplete script. I was able to solve it through some hack on the _parsedotname function. I am not sure how portable is my hacking due to the issue on convert the '.' into absolute path but it works in my machine. Below are my changes(yeah, you see lots of print statement which I use it to understand the code flow...)
def _parsedotname(self,pre=None):
#returns (dottedname, nexttoken)
name = []
absolute_relative_path = False
if pre is None:
tokentype, token, indent = self.next()
#print tokentype, token, indent
if tokentype == 51 and token == '.':
import os
import sys
#print os.path.abspath(os.curdir)
fullpath = os.path.abspath(os.curdir)
paths = fullpath.split(os.path.sep)
n_ = -1
#print fullpath
pyexeindex = sys.path.index(os.path.dirname(sys.executable))
#print sys.path[pyexeindex:]
while fullpath not in sys.path[pyexeindex:]:
fullpath = os.path.sep.join(paths[:n_])
#print fullpath
n_ -= 1
if fullpath == '':
return ('', token)
absolute_relative_path = True
name = '.'.join(paths[n_+1:])
#print name
elif tokentype != NAME and token != '*':
#print 'should not here'
return ('', token)
else: token = pre
if '.' in name:
name = name.split('.')
else:
name.append(token)
while True:
if not absolute_relative_path:
tokentype, token, indent = self.next()
if token != '.': break
tokentype, token, indent = self.next()
if not absolute_relative_path:
if tokentype != NAME: break
else:
absolute_relative_path = False
if tokentype == NAME and token == 'import':
return (".".join(name), token)
name.append(token)
return (".".join(name), token)
Now, it worked for both:
from . import module
from .moduleA import moduleB
I suppose you're using the vim internal pythoncomplete.
As I wrote here: Python docstring with vim pythoncomplete is not displaying newlines for my own class functions
pythoncomplete is a pretty simple tool, that does most of its completions by executing the import statements (Which is pretty dangerous by the way). Solving it is probably not the best idea, because I'm currently trying to do that (writing a good python auto-completion).
But I don't think my version will be ready to do what you want in another one or two months, but it's already really far, I will tell you when I'm ready.

XMODEM for python

I am writing a program that requires the use of XMODEM to transfer data from a sensor device. I'd like to avoid having to write my own XMODEM code, so I was wondering if anyone knew if there was a python XMODEM module available anywhere?
def xmodem_send(serial, file):
t, anim = 0, '|/-\\'
serial.setTimeout(1)
while 1:
if serial.read(1) != NAK:
t = t + 1
print anim[t%len(anim)],'\r',
if t == 60 : return False
else:
break
p = 1
s = file.read(128)
while s:
s = s + '\xFF'*(128 - len(s))
chk = 0
for c in s:
chk+=ord(c)
while 1:
serial.write(SOH)
serial.write(chr(p))
serial.write(chr(255 - p))
serial.write(s)
serial.write(chr(chk%256))
serial.flush()
answer = serial.read(1)
if answer == NAK: continue
if answer == ACK: break
return False
s = file.read(128)
p = (p + 1)%256
print '.',
serial.write(EOT)
return True
There is XMODEM module on PyPi. It handles both sending and receiving of data with XModem. Below is sample of its usage:
import serial
try:
from cStringIO import StringIO
except:
from StringIO import StringIO
from xmodem import XMODEM, NAK
from time import sleep
def readUntil(char = None):
def serialPortReader():
while True:
tmp = port.read(1)
if not tmp or (char and char == tmp):
break
yield tmp
return ''.join(serialPortReader())
def getc(size, timeout=1):
return port.read(size)
def putc(data, timeout=1):
port.write(data)
sleep(0.001) # give device time to prepare new buffer and start sending it
port = serial.Serial(port='COM5',parity=serial.PARITY_NONE,bytesize=serial.EIGHTBITS,stopbits=serial.STOPBITS_ONE,timeout=0,xonxoff=0,rtscts=0,dsrdtr=0,baudrate=115200)
port.write("command that initiates xmodem send from device\r\n")
sleep(0.02) # give device time to handle command and start sending response
readUntil(NAK)
buffer = StringIO()
XMODEM(getc, putc).recv(buffer, crc_mode = 0, quiet = 1)
contents = buffer.getvalue()
buffer.close()
readUntil()
I think you’re stuck with rolling your own.
You might be able to use sz, which implements X/Y/ZMODEM. You could call out to the binary, or port the necessary code to Python.
Here is a link to XMODEM documentation that will be useful if you have to write your own. It has detailed description of the original XMODEM, XMODEM-CRC and XMODEM-1K.
You might also find this c-code of interest.
You can try using SWIG to create Python bindings for the C libraries linked above (or any other C/C++ libraries you find online). That will allow you to use the same C API directly from Python.
The actual implementation will of course still be in C/C++, since SWIG merely creates bindings to the functions of interest.
There is a python module that you can use -> https://pypi.python.org/pypi/xmodem
You can see the transfer protocol in http://pythonhosted.org//xmodem/xmodem.html

Categories

Resources