lxml python 2.5 ElementMaker syntax error - python

I have the following code:
from lxml.builder import ElementMaker
E = ElementMaker()
params = [E.param('1'), E.param('2')]
E.p( *params, count='2')
This works fine in python 2.6, but when I run it with python 2.5, I get the following error:
E.p( *params, count='2')
^ SyntaxError: invalid syntax
I can't figure out why this is happening. Why does 2.5 throw this error? How can I fix it?

You can't follow * with keyword arguments in Python before 2.6. You can try:
E.p(*params, **{'count': '2'})
or if you'd rather:
E.p(*params, **dict(count='2')})

Related

When porting python 2.5 to 3.X, how can I replace "from <module> import *"?

I've got a python 2.5 package with the following structure:
Config.py contains the following line:
from CommonDefines import *
Runnning this code in 3.7 gives the following exception:
File "../../.\ConfigLib\Config.py", line 7, in
from CommonDefines import * ModuleNotFoundError: No module named 'CommonDefines'
Replacing that line with:
from .CommonDefines import *
... works in 3.7 but gives the following error in 2.5:
SyntaxError: 'import *' not allowed with 'from .'
Is there a way to write this line so that works in both 2.5 and 3.X?
EDIT:
The following doesn't work, since the second import triggers a syntax error in 2.5
try:
from CommonDefines import *
except:
from .CommonDefines import *
SyntaxError: 'import *' not allowed with 'from .'
I would just use a proper name-by-name import, but this can be done in a hacky way, for your personal use, using exec:
try:
from CommonDefines import *
except ModuleNotFoundError:
exec('from .CommonDefines import *')
You can even swap them and catch the SyntaxError.

rpy2 in python SyntaxError: invalid syntax for as.ape.AAbin

Im using rpy2 package in python. It's a package for R programming via python. when i use 'as' function in python it consider it a syntax error. It works fine in R. I imported all necessary packages. Is there any command to replace as.apes.
from rpy2.robjects.packages import importrcode
utils = importr('utils')
utils.chooseCRANmirror(ind=1)
base = importr('base')
methods= importr('methods')
packnames = ('ape', 'aphid','methods')
apes= importr("ape")
g1 = as.apes.AAbin("EL---DSD-ILPELLATLARTHDLNK----VGPAHYDLFAKVLM")
g1 = as.apes.AAbin("EL---DSD-ILPELLATLARTHDLNK----VGPAHYDLFAKVLM")
^
SyntaxError: invalid syntax
problem solved! I changed the whole way of calling R in python. I used wrapping,I used my whole R script as follows:
from rpy2.robjects.packages import SignatureTranslatedAnonymousPackage
string = """
#paste your script here
"""
powerpack = SignatureTranslatedAnonymousPackage(string, "powerpack")
refer to thepackage documentation for further details.

trouble with setting a SNMP attribute using pythone and netsnmp library

I am trying to use the netsnmp library to snmpset a attribute I get an error
robm#PC2303VM:~/code/python/snmp$ ./tester2.py
File "./tester2.py", line 5
ipaddr=netsnmp.Varbind('.1.3.6.1.2.1.69.1.3.1.0',172.168.100.2,'IPADDRESS')
^
SyntaxError: invalid syntax
now from the command line
snmpset -v2c -c private 10.1.1.8 .1.3.6.1.2.1.69.1.3.1.0 a 172.168.100.2
works just fine and quoting "172.168.100.2" gives a different error
TypeError: expected string or Unicode object, NoneType found
#!/usr/bin/python
import netsnmp
ipaddr=netsnmp.Varbind('.1.3.6.1.2.1.69.1.3.1.0',172.168.100.2,'IPADDRESS')
netsnmp.snmpset(ipaddr, Version=2, DestHost="10.1.1.8", Community="private")
filename=netsnmp.Varbind('.1.3.6.1.2.1.69.1.3.2.0', './robertme/Q2Q_REL_7_2_2_2015_04_07_T1935.bin','STRING')
netsnmp.snmpset(filename, Version=2, DestHost='10.1.1.8', Community='private' )
any Ideas on how to format that correctly?

with statement yields "Invalid syntax" error in Python 2.4

I have some code written in Python 2.7 like so :
if (os.path.exists('/path/to/my/file/somefile.txt')):
with open('/path/to/my/file/somefile.txt', 'r') as readfile:
firstline = readfile.readline()
return firstline
When I try to run this on a system that has python 2.4, I get and Invalid Syntax error:
with open('/path/to/my/file/somefile.txt', 'r') as readfile:
^
SyntaxError: invalid syntax
What am I doing wrong here?
There is no 'with' statement aka context managers in Python 2.4.
Python 2.4 is more than 10 years old.
Upgrade to Python 2.7 or 3.3.
Since with doesn't exist, could you try this instead?
import os
if (os.path.exists('/root/testing/test123.txt')):
readfile = open('/root/testing/test123.txt', 'r')
teststr = readfile.readline()
print teststr #or 'return' if you want that

does the django-firebird 1.5.0.rc.2 driver work with Python 3.3?

I am trying to use Firebird with django but when I install it with .
pip install django-firebird
I get the following error
File "C:\Python33\Lib\site-packages\firebird\base.py", line 9
except ImportError, e:
^
SyntaxError: invalid syntax
File "C:\Python33\Lib\site-packages\firebird\creation.py", line 76
print "_rollback_works"
^
SyntaxError: invalid syntax
Example code in the base.py is as follows :-
except Database.IntegrityError, e:
raise utils.IntegrityError, utils.IntegrityError(*self.error_info(e, query, param_list[0])), sys.exc_info()[2]
I am running Windows, Python 3.3 and Django 1.5.
Is this a syntax change with Python 3?
The package says it works with python 2.6+
Regards
Any ideas?
In Python 2:
except ImportError, e:
print "_rollback_works"
In Python 3:
//Exception handling syntax changes slightly, "as"
except ImportError as err:
//print is now a function print()
print ("_rollback_works")
That's why you get that errors.

Categories