-
Notifications
You must be signed in to change notification settings - Fork 31
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added __repr__ and a helper function #10
base: master
Are you sure you want to change the base?
Conversation
Thanks for the contribution! Unfortunately, this code breaks many things:
meta:
id: rec
seq:
- id: some
type: u1
instances:
self_ref:
value: _root
|
kaitaistruct.py
Outdated
@@ -27,6 +27,13 @@ def __init__(self, stream): | |||
def __enter__(self): | |||
return self | |||
|
|||
def _reprGeneratorForAllProps(self): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this name doesn't comply with pep8.
Forgive me for being outright dismissive but... is |
|
I just fixed the style/pep8, didnt change how it works. Better? |
I have added a guard against recursive objects. |
Broadened scope to instances. @arekbulski @GreyCat @funkyfuture could you test and review it, it seems it's ready. |
kaitaistruct.py
Outdated
@@ -15,6 +15,18 @@ | |||
# | |||
__version__ = '0.8' | |||
|
|||
from RecursionSafe import RecursionSafe | |||
|
|||
recSafe=RecursionSafe() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
does flake8 not complain about that symbol name?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
recSafe will be fixed to rec_safe
. I don't use flake.
kaitaistruct.py
Outdated
"""Generator to use in own __repr__ functions.""" | ||
return ( | ||
"".join(( str(k), "=", repr(getattr(self, k)) )) | ||
for k in dir(self) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i don't know if it's usable in this context, but vars
is often inferior over dir
and might make test in the following line unnecessary.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
vars
doesn't return properties.
kaitaistruct.py
Outdated
return ( | ||
"".join(( str(k), "=", repr(getattr(self, k)) )) | ||
for k in dir(self) | ||
if k[0] != "_" and not hasattr(KaitaiStruct, k) and not isinstance(getattr(self, k), type) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
k.startswith('_')
would be more expressive.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's longer and twice slower.
@@ -26,6 +38,16 @@ def __enter__(self): | |||
def __exit__(self, *args, **kwargs): | |||
self.close() | |||
|
|||
def __repr__(self): | |||
return "".join( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
these are too much line-breaks for my gusto.
setup.py
Outdated
@@ -44,4 +44,6 @@ | |||
], | |||
keywords='kaitai,struct,construct,ksy,declarative,data structure,data format,file format,packet format,binary,parser,parsing,unpack,development', | |||
py_modules=["kaitaistruct"], | |||
requires=["RecursionSafe"], | |||
dependency_links=["git+https://github.com/KOLANICH/RecursionSafe.py.git"], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i wouldn't trust a python library with camelcase naming and .py
suffix. ;-)
also, i wouldn't outsource 45 lines of code to create a dependency, but rather put it in an utils
module.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also:
Requirements
Python 3.
Which is, obviously, a deal-breaker for inclusion here anyway :(
Python 2 is dead, stop raping its corpse.
Unfortunately, I see the exact opposite :(
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
my 2 cents.
8c117dd
to
9d712b1
Compare
I dont even understand how this code works, nor why its so lengthy. Also its not providing different str and repr versions. I would suggest considering using Construct as reference:
|
I appreciate the initiative, but honestly I dont like the implementation. If you would be willing to wait, then I will offer alternative implementation based on Construct. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I dont like this implementation overall, but I dont have any specific points to make.
There were some errors, I have fixed them. |
8192392
to
d4aee68
Compare
@GreyCat @arekbulski I was advised with a good replacement for a lock and truncation which are already a part of python stdlib :) |
reprlib is missing on python 2, but I guess may be backported and put into pip, the name is free https://pypi.python.org/pypi/reprlib |
I have issues with this implementation:
|
Again: we can try to backport it and put into pip.
fixed. Thank you for bringing it up.
It does, it could. But reprlib provides recursion lock and truncation, so I guess there will be no infinite recursions and rendering too much values of sequences. About reading large files into memory which can cause consuming all the memory in machine - I guess we should map them, not cache them and use references to them instead of copies (I wonder if it is possible in python), this would lead into consuming only a single page. Maybe we need a |
But thats not what I said or meant. We could have the compiler generate KaitaiStruct-s that have something similar to slots, a list of names, to iterate that instead of filtering dir: class Enum0(KaitaiStruct):
__slotsalike__ = ["pet_1","pet_2"]
def _read(self):
self.pet_1 = self._root.Animal(self._io.read_u4le())
self.pet_2 = self._root.Animal(self._io.read_u4le()) then - for k in dir(self)
- if k[0] != "_" and not hasattr(KaitaiStruct, k) and not isinstance(getattr(self, k), type)
+ for k in __slotsalike__:
+ yield getattr(self, k) This would not only have better performance, if you care about that in this case that is, but it might also be useful to end users as they might have a use for a list of field names. |
That does not address the issue I meant. You could have a pointer instance that refers to outside of stream size, which will cause failure if evaluated. Its not about the size of the output. |
Out of scope - its responsibility of a ksy developer to have all the needed
I agree. I guess we need non-volatile fields in And I guess that collection of some of this stuff can be done in runtime without any changes to compiler. |
@GreyCat Could you share your position with us? How about we have the compiler attach a list of IDs to each generated Struct, similar to slots? |
Compiler actually already does something similar when doing We generate an array called something like "seq_fields", i.e.:
Could you explain, what's wrong with current solution - i.e. is it's reflection slow, unreliable, or something else is the matter? As far as I have guessed, |
Non-volatile fields are the fields (members in
we process strings to determine if a name to be included, it's ugly, and may be slow One more considiration: I guess we'll have inheritance somewhen, so the thing should be compatible to it without much overhead. |
SEQ_FIELDS is exactly what I am talking about. The only issue with it is that it should be included even without debug mode and available in Python. On Kolanich behalf: non-volatile is just a synonym for cached (like instances are cached). |
Hi, what's the state of this PR? I'm interest in this as well as the default string representation is not really useful at the moment |
The status of this PR is that I personally use it, but in a form of a separate branch on top of |
38e38f3
to
bbad79a
Compare
No description provided.