AttributeErrors, Tuples and other things nice - python

I can't say I fully understand the script, because classes go beyond me as yet. Anyway, I've downloaded the py-omegle module from here . And I don't seem to be able to get it to run.
Hoping that I don't need to post the whole class including functions, the part in particular that I'm having trouble with pertains to urllib2 - so I guess It's not too specific an issue - the line that causes all of the issues is:
self.connector = urllib2.build_opener(processor),urllib2.HTTPHandler(debuglevel=1)
and it's not letting me:
#omegle.py
[ln33] self.connector.addheaders = [
[ln34] ('User-agent',user_agent)
[ln35] ]
# or
[ln98] self.id = self.connector.open(self.url+'start',data={}).read().strip('"')
Both return AttributeError:
AttributeError: 'tuple' object has no attribute 'addheaders'
# and further down
AttributeError: 'tuple' object has no attribute 'open'
Could someone please explain how to fix this? I'm sure it has something to do with the first line I posted. The entire source of the ONLY file in this module can be accessed here.

I think it's a case of misplaced parentheses.
The first line:
self.connector = urllib2.build_opener(processor),urllib2.HTTPHandler(debuglevel=1)
creates a tuple consisting of
urllib2.build_opener(processor)
and
urllib2.HTTPHandler(debuglevel=1)
And then assigns this tuple to self.connector.

Related

Python: 'object has no attribute ' (pyTsetlinMachine)

I was wondering if I could solve the problem described at https://github.com/cair/pyTsetlinMachine/issues/6 "myself".
I added a running example to the github issue.
The following error occurs
File "/mnt/c/ProjectsGit/BreastCancerDemo_pkl.py", line 42, in <module>
tm2.set_state(state)
File "/home/unix/miniconda3/lib/python3.8/site-packages/pyTsetlinMachine/tm.py", line 351, in set_state
for i in range(self.number_of_classes):
AttributeError: 'MultiClassTsetlinMachine' object has no attribute 'number_of_classes'
The class 'MultiClassTsetlinMachine' seems to have such as attribute?
https://github.com/cair/pyTsetlinMachine/blob/0c1ff1d43e1dd466ae0e41d50a4bde94bb36fedc/pyTsetlinMachine/tm.py#L396
Is it the case that the attribute 'number_of_classes' is not stored? but could be recovered by the dimension of the vector?
Any ideas? Hints?
You can see in the source code that the attribute gets set (the first time) you run fit, and does not get stored or retrieved in get_state or set_state. The code in that link is self.number_of_classes = int(np.max(Y) + 1), so I guess Y is supposed to be numerically encoded and one-dimensional. So yes, setting it yourself (not the dimension of the vector, but the number of unique values) should be enough. You could pickle that number together with the state, if that's more convenient.
That should at least be a good start, but I'm not familiar enough with the package to know if everything will work correctly from there.

Django Unhasable Type List even though im passing in an object ?(many to many field)

I am trying to run a migration script for my django app, but I keep getting TypeError: unhashable type: 'list' even though I am clearly passing in an Object:
I get:
error line 87, in buildCisc
c.exclusions.add(exclus)
line 944, in add
self._add_items(
line 1119, in _add_items
target_ids = self._get_target_ids(target_field_name, objs)
line 1059, in _get_target_ids
target_ids.add(target_id)
TypeError: unhashable type: 'list'
when I run the following code
...
for ex in exclusionList:
if len(Course.objects.filter(ID=ex)) > 0: # exclusion course already exsists
exclus = Course.objects.filter(ID=ex)
c.exclusions.add(exclus[0])
else: # exclusion course does not exsist yet so we must create it first
exclus = Course(ID=ex)
exclus.save()
c.exclusions.add(exclus) #this is line 87 causing the error
where c is a Course object create in prior code, exclusions is a many to many field from Course to itself, 'ex' is just a string.
if I try using exclus = Course.objects.create(ID=ex) instead that also gives the same error. The error seems to be saying that the exclus that I am passing in to c.exclusions.add is a list, but it is very clearly an object. I even tried switching exclus to exclus[0] to see if it somehow thought it was a list, but this gave me error: Course Object not iterable so it is saying it is of type Object, so I am very confused with error message. any ideas?
edit: I believe to have solved the issue, the exclus = Course(ID=ex) was the problem, specifically ex. For some reason in my read in phase, it got set to a list somehow and not a string like I thought it was
Since c is a Course object, it contains a list and that's the part that cannot be hashed. Try using .append() instead of .add(). The add() method is for sets and those require that the object be hashed since it needs to compare items for uniqueness.

AttributeError: 'module' object has no attribute 'doc

i am a beginner and i trying to model system dynamic model using python programming.the problem is when i trying to print the components of the sd model, the error message comes out like this:
"AttributeError: 'module' object has no attribute 'doc'"
my code:
import pysd
educationmodel = pysd.read_vensim('Education.mdl')
print educationmodel.components.doc()
As far as understand from the git repo, the doc() method is inside Class PySD. Also, read_vensim returns an instance of this class.
So your problem should get solved if you directly use educationmodel.doc().
That may be my fault - I had to move the .doc() function to the model object instead of the components object as a way to work towards including Vensim macros properly. If it's still an issue, may want to update to the latest release (0.7.4). If that doesn't help either, then we may have to fix something. =)

can you use pydictionary in google app engine?

I'm trying to use pyDictionary in a google app engine application but I get an error. I don't get this error when I use it outside the google application. I've added it as a third party library (properly) so it actually imports it without errors so I don't know why.
here's the error:
searching.py", line 63, in getkey assert isinstance(word.keys, object)
AttributeError: 'NoneType' object has no attribute 'keys'
and here's the code for that function:
def getkey(term):
dictionary = PyDictionary()
word = dictionary.meaning(term)
assert isinstance(word.keys, object)
results = word.keys()
newresults = []
for result in results:
newresults.append(str(result))
return newresults[0]
it works outside the app engine project, but not inside...
You should go back and look at your the exception and try and understand what it is telling you.
searching.py", line 63, in getkey assert isinstance(word.keys, object)
AttributeError: 'NoneType' object has no attribute 'keys'
Let's trace it back through your code.
For that error to occur means that value referred to by word is None.
This means that the result of word = dictionary.meaning(term)
Is None.
So you should therefore look at what the value of term is.
Are you validating that term is meaningful anywhere ? If it is, what is the expected result of dictionary.meaning(term) and then investigate why you don't get the expected result.
Some basic debugging would go a long way here, helping you diagnose the issue.

python regular expressions function not working

I am learning python so this may be a simple question for alot of you and i hope to get some help in understanding what is going wrong.
I am trying to create a function that searches a text for phone numbers.
import re
def findPhoneNumber(a):
b = re.compile(r'\d{3}-\d{3}-\d{4}')
c = b.search(a)
return c.group()
findPhoneNumber('123')
I am getting this error:
AttributeError: 'NoneType' object has no attribute 'group'
so for my understandings, the c variable is not getting any values associated and thus is returning this error.
Can anyone explain what I am doing wrong here?
Your example "123" does not match any phone number, you need "123-456-7890".
So in your example c==None. You have to test that c is an actual match object with if(c) before trying to access c.group()
If your regex doesn't match anything c would be None giving you the next exception:
AttributeError: 'NoneType' object has no attribute 'group'
You just need to handle that exception
def findPhoneNumber(a):
try:
return re.search(r'\d{3}-\d{3}-\d{4}', a).group()
except AttributeError:
return None
Also it has little sense to compile the regex inside the function to use it just one time

Categories

Resources