Expression problem.

N

Nethirlon .

Hi,

I am having trouble with an expression.

I have the following line of code:

self.failUnless(c.as == 65215)

What happens when you compile this is that you get a syntax error.
This is because as has been made a keyword. failUnless is from the
module unittest.

Now my problem is this. the ".as" is no where defined in the code. The
code builds just fine if you removed it. But the origigal programmer
must have put it in for a reason. I just cant understand what that is.

Can anyone point me into the right direction? was ".as" before it
became a keyword some kind of string manipulator?

Let me know if you need more info.
Thanks
 
C

Chris Rebert

Hi,

I am having trouble with an expression.

I have the following line of code:

self.failUnless(c.as == 65215)

What happens when you compile this is that you get a syntax error.
This is because as has been made a keyword. failUnless is from the
module unittest.

Now my problem is this. the ".as" is no where defined in the code. The
code builds just fine if you removed it. But the origigal programmer
must have put it in for a reason. I just cant understand what that is.

Can anyone point me into the right direction? was ".as" before it
became a keyword some kind of string manipulator?

Nope, it was just normal attribute syntax. I would assume the original
author just named some object attribute "as" for whatever reason, but
the fact that removing it doesn't cause a test failure is just
bizarre. Perhaps you should examine the code for c's class (and/or its
ancestors)? (Sounds like you might have already done that though, in
which case: o_O *shrug*)

Cheers,
Chris
 
S

Sebastiaan de Haan

Thank you Chris,

I'll try and find the attribute in the code. That was my conclusion
aswell... The original author must have defined it somewhere...

Thanks.
 
P

Peter Otten

Sebastiaan said:
Thank you Chris,

I'll try and find the attribute in the code. That was my conclusion
aswell... The original author must have defined it somewhere...

Don't forget to check whether the object's class (or any of its bases) has a
__getattr__() or __getattribute__() method.
.... def __getattr__(self, name):
.... return 42
.... File "<stdin>", line 1
a.as
^
SyntaxError: invalid syntax

Note tha you can still access such an attribute using getattr()
42

Peter
 
N

Nethirlon .

Don't forget to check whether the object's class (or any of its bases) has a
__getattr__() or __getattribute__() method.


...     def __getattr__(self, name):
...             return 42
...>>> a = A()

  File "<stdin>", line 1
    a.as
       ^
SyntaxError: invalid syntax

Note tha you can still access such an attribute using getattr()


42

Peter

Thank you Peter,

While searching the document I found the following code:

class Open(dpkt.Packet):
__hdr__ = (
('v', 'B', 4),
('as', 'H', 0),
('holdtime', 'H', 0),
('identifier', 'I', 0),
('param_len', 'B', 0)
)

So, I am new at programming with Python, but doing my best to grasp
the concept here. From what I am understanding is that the __hdr__ is
something that the original programmer cameup with for him self. I am
just curious as to weather the "as" in this piece of code is the one I
am searching for.

Thanks in advance.
 
P

Peter Otten

Nethirlon said:
Thank you Peter,

While searching the document I found the following code:

class Open(dpkt.Packet):
__hdr__ = (
('v', 'B', 4),
('as', 'H', 0),
('holdtime', 'H', 0),
('identifier', 'I', 0),
('param_len', 'B', 0)
)

So, I am new at programming with Python, but doing my best to grasp
the concept here. From what I am understanding is that the __hdr__ is
something that the original programmer cameup with for him self. I am
just curious as to weather the "as" in this piece of code is the one I
am searching for.

Side note: if the code you have questions about is publicly available it's
always a good idea to give the url. I am assuming that you are referring to
an older version to this beast:

http://code.google.com/p/dpkt/source/browse/trunk/dpkt/bgp.py

Here's where your problem was fixed/adjusted to newer Python versions:
http://code.google.com/p/dpkt/source/detail?r=51

The __hdr__ is indeed an invention of the author of the package, and is feed
to the metaclass* of dpkt.Packet. The metaclass uses it to create __slots__
that are filled dynamically in Packet.__init__().

I recommend that you read the docstring of the Packet class

http://code.google.com/p/dpkt/source/browse/trunk/dpkt/dpkt.py

but only bother about the implementation if you cannot avoid it.
You can always have a second look after you have gained some Python
experience.

Peter

(*) Every class in Python is an instance of its metaclass, i. e. the
relation between metaclass and class is the same as between class and
instance. Custom metaclasses are a powerful feature, but tend to make Python
code harder to grasp.
 
N

Nethirlon .

Side note: if the code you have questions about is publicly available it's
always a good idea to give the url. I am assuming that you are referring to
an older version to this beast:

http://code.google.com/p/dpkt/source/browse/trunk/dpkt/bgp.py

Here's where your problem was fixed/adjusted to newer Python versions:http://code.google.com/p/dpkt/source/detail?r=51

The __hdr__ is indeed an invention of the author of the package, and is feed
to the metaclass* of dpkt.Packet. The metaclass uses it to create __slots__
that are filled dynamically in Packet.__init__().

I recommend that you read the docstring of the Packet class

http://code.google.com/p/dpkt/source/browse/trunk/dpkt/dpkt.py

but only bother about the implementation if you cannot avoid it.
You can always have a second look after you have gained some Python
experience.

Peter

(*) Every class in Python is an instance of its metaclass, i. e. the
relation between metaclass and class is the same as between class and
instance. Custom metaclasses are a powerful feature, but tend to make Python
code harder to grasp.

Peter,

Thank you very much, I did not know that the code was available
online, and also did not know that the author updated it online.

The package I was using came from the openSuse python repository,
given to me by one of the maintainers of that repository.

I am having a hard time understanding the concept of classes, so I
think that I should focus on that first, before continueing to try and
contribute.

Thank you very much for your help!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,169
Messages
2,570,919
Members
47,460
Latest member
eibafima

Latest Threads

Top