I got an error while run python proxy.py
$ python proxy.py
INFO - [Sep 28 14:59:19] getting appids from goagent plus common appid pool!
Traceback (most recent call last):
File "proxy.py", line 2210, in <module>
main()
File "proxy.py", line 2180, in main
pre_start()
File "proxy.py", line 2157, in pre_start
common.set_appids(get_appids())
File "proxy.py", line 94, in get_appids
fly = bytes.maketrans(
AttributeError: type object 'str' has no attribute 'maketrans'
The proxy.py file in https://code.google.com/p/smartladder/,
def get_appids():
fly = bytes.maketrans(
b"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ",
b"nopqrstuvwxyzabcdefghijklmNOPQRSTUVWXYZABCDEFGHIJKLM"
)
f = urllib.request.urlopen(url="http://lovejiani.com/v").read().translate(fly)
d = base64.b64decode(f)
e = str(d, encoding='ascii').split('\r\n')
random.shuffle(e)
return e
You are running code written for Python 3, with Python 2. This won't work.
maketrans is a classmethod on the bytes built-in type, but only in Python 3.
# Python 3
>>> bytes
<class 'bytes'>
>>> bytes.maketrans
<built-in method maketrans of type object at 0x10aa6fe70>
In Python 2, bytes is an alias for str, but that type does not have that method:
# Python 2.7
>>> bytes
<type 'str'>
>>> bytes.maketrans
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: type object 'str' has no attribute 'maketrans'
Run your code with Python 3 instead, or translate all code in this project to Python 2; the latter requires in-depth knowledge of how Python 2 and 3 differ and is likely a major undertaking.
Just the illustrated function, translated to Python 2, would be:
import string
import urllib2
import base64
import random
def get_appids():
fly = string.maketrans(
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ",
"nopqrstuvwxyzabcdefghijklmNOPQRSTUVWXYZABCDEFGHIJKLM"
)
f = urllib2.urlopen("http://lovejiani.com/v").read().translate(fly)
d = base64.b64decode(f)
e = unicode(d, encoding='ascii').split(u'\r\n')
random.shuffle(e)
return e
Related
I have this error ValueError: Unknown format code 'x' for object of type 'str'.
The probleme is the version of my python(python 2.7) but I can't upgrade it.
Someone has any idea how to execute this line of code in python 2.map('{:02x}'.format, bytes_addr) See bellow the two functions uses it.
map('{:02x}'.format, bytes_addr)
def get_mac_addr(mac_raw):
byte_str = map('{:02x}'.format, mac_raw)
mac_addr = ':'.join(byte_str).upper()
return mac_addr
def format_multi_line(prefix, string, size=80):
size -= len(prefix)
if isinstance(string, bytes):
string = ''.join(r'\x{:02x}'.format(byte) for byte in string)
if size % 2:
size -= 1
return '\n'.join([prefix + line for line in textwrap.wrap(string, size)])
Output
Traceback (most recent call last):
File "basic_sniff1.py", line 80, in <module>
main()
File "basic_sniff1.py", line 54, in main
eth = ethernet_head(raw_data)
File "basic_sniff1.py", line 23, in ethernet_head
dest_mac = get_mac_addr(dest)
File "basic_sniff1.py", line 6, in get_mac_addr
byte_str = map('{:02x}'.format, mac_raw)
ValueError: Unknown format code 'x' for object of type 'str'
I need help to fix this code:
import urllib.request,urllib.parse,urllib.error
fhand = urllib.request.urlopen("http://data.pr4e.org//romeo.txt")
counts = dict ()
for line in fhand:
lines = line.decode.split()
for words in lines:
counts[words] = counts.get(words,0)+1
print(counts)
I am getting this error while running this code:
C:\Users\g.p\AppData\Local\Programs\Python\Python37-32>py urllib2.py
Traceback (most recent call last):
File "urllib2.py", line 5, in <module>
lines = line.decode.split()
AttributeError: 'builtin_function_or_method' object has no attribute 'split'
you should run decode function, otherwise, it will be the built-in function not str, so you cannot split the function
You should write like this:
lines = line.decode().split()
For more info: Link
i've copied a python code from a guide:
class Carta:
ListaSemi=["Fiori","Quadri","Cuori","Picche"]
ListaRanghi=["impossibile","Asso","2","3","4","5","6",\
"7","8","9","10","Jack","Regina","Re"]
def __init__(self, Seme=0, Rango=0):
self.Seme=Seme
self.Rango=Rango
def __str__(self):
return (self.ListaRanghi[self.Rango] + " di " + self.ListaSemi[self.Seme])
def __cmp__(self, Altro):
#controlla il seme
if self.Seme > Altro.Seme: return 1
if self.Seme < Altro.Seme: return -1
#se i semi sono uguali controlla il rango
if self.Rango > Altro.Rango: return 1
if self.Rango < Altro.Rango: return -1
return 0
when i call from shell:
>>> Carta1=Carta(1,11)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'module' object is not callable
I'm using python version 2.7.
what's wrong??
thanks
I assume that the snippet above is saved as Carta.py and that you ran in your interactive shell:
>>> import Carta
>>> Carta1=Carta(1,11)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'module' object is not callable
This way, you try to call/instantiate the module instead of the class inside it. You have basically two options to fix this, changing either the import or the constructor call:
>>> from Carta import Carta
>>> Carta1=Carta(1,11)
>>> import Carta
>>> Carta1=Carta.Carta(1,11)
If you rename either the module file or the class so that you can distinguish them better, it becomes clear.
I'm having issues accessing a COM object using the Python win32com module, which I don't have when using VBA. See usage/error below. The other parts of the object are functioning OK, but I can't get any of the parameters out of the ResultSet object.
>>> import win32com.client
>>> Aquator = win32com.client.gencache.EnsureDispatch("Aquator.Application")
>>> db = Aquator.LoadDatabase(r"D:\Shared", "AquatorExcel.mdb")
>>> project = Aquator.LoadProject(db, "A simple model", False, False)
>>>
>>> project.ModelRunStart()
<win32com.gen_py.Aquator Water Resource Simulation._ModelRun instance at 0x15264
824>
>>> Aquator.ActiveProject.ModelRuns.Count
1
>>> Aquator.ActiveProject.ModelRuns.Item(1).ResultSet
<win32com.gen_py.Aquator Water Resource Simulation._IResultSet instance at 0x149
19000>
>>> Aquator.ActiveProject.ModelRuns.Item(1).ResultSet.Cost
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Program Files\Pythonxy\Pythonxy 2.7\Python27\lib\site-packages\win32c
om\client\__init__.py", line 463, in __getattr__
return self._ApplyTypes_(*args)
File "C:\Program Files\Pythonxy\Pythonxy 2.7\Python27\lib\site-packages\win32c
om\client\__init__.py", line 456, in _ApplyTypes_
self._oleobj_.InvokeTypes(dispid, 0, wFlags, retType, argTypes, *args),
pywintypes.com_error: (-2147352573, 'Member not found.', None, None)
The equivalent code in the VBA editor within the application functions correctly, returning a float:
Public Sub Test()
MsgBox Aquator.ActiveProject.ModelRuns.Item(1).ResultSet.Cost
End Sub
As far as I can tell, the EnsureDispatch command has done it's job correctly, and recognises the property (along with others, also inaccessible):
>>> Aquator.ActiveProject.ModelRuns.Item(1).ResultSet._prop_map_get_.keys()
['WaterBalanceMl', 'RunDate', 'Parameters', 'DoublePrecision', 'WarningCount', '
FinishDate', 'AmountLost', 'SinglePrecision', 'Status', 'StartDate', 'Descriptio
n', 'AmountLeaked', 'FailureCount', 'AmountAdded', 'ErrorCount', 'AmountStored',
'Name', 'WaterBalancePercent', 'InfoValueList', 'Results', 'States', 'Cost', 'A
mountRemoved', 'Sequences', 'Duration', 'InfoNameList', 'RunTime']
>>> Aquator.ActiveProject.ModelRuns.Item(1).ResultSet.meh
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Program Files\Pythonxy\Pythonxy 2.7\Python27\lib\site-packages\win32c
om\client\__init__.py", line 462, in __getattr__
raise AttributeError("'%s' object has no attribute '%s'" % (repr(self), attr
))
AttributeError: '<win32com.gen_py.Aquator Water Resource Simulation._IResultSet
instance at 0x15175888>' object has no attribute 'meh'
I have tried using .GetCost() (a la Setting a property using win32com), to no avail ("object has no attribute...").
What am I doing wrong here?
It seems the problem is with the win32com module. The equivilent code using the comtypes module functions correctly:
>>> from comtypes.client import CreateObject
>>> Aquator = CreateObject("Aquator.Application")
>>> Database = Aquator.LoadDatabase(r"D:\Shared", "AquatorExcel.mdb")
>>> Project = Aquator.LoadProject(Database, "A simple project", False, False)
>>> ModelRun = Project.ModelRunStart()
>>> print ModelRun.ResultSet.Cost
133432.20
Hello I have an Error and I don´t the reason:
>>> class Fruits:pass
...
>>> banana = Fruits()
>>> banana.color = 'yellow'
>>> banana.value = 30
>>> import pickle
>>> filehandler = open("Fruits.obj",'w')
>>> pickle.dump(banana,filehandler)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python31\lib\pickle.py", line 1354, in dump
Pickler(file, protocol, fix_imports=fix_imports).dump(obj)
TypeError: must be str, not bytes
>>>
I don´t know how to solve this error because I don´t understand it.
Thank you so much.
You have to open your filehandler in binary mode, use wb instead of w:
filehandler = open(b"fruits.obj","wb")