[x3d-public] Thoughts for x3d.py str() calls
John Carlson
yottzumm at gmail.com
Thu Oct 19 09:16:01 PDT 2023
There are 239 instances of "str" in X3duomToX3dPythonPackage.xslt. 208 of
them are not related to isinstance
Please advise before I go sailing in modifying them to another function.
This looks like a major modification.
On Thu, Oct 19, 2023 at 11:09 AM John Carlson <yottzumm at gmail.com> wrote:
> Redeclaring the float class doesn't seem to work, isinstance fails
>
> Monkey patching doesn't work:
>
> def custom_float_str(value):
> return f"{value:.5g}"
>
> # Monkey-patch the float class
> float.__str__ = custom_float_str
>
> Overriding the str function doesn't work, because that overrides the str
> type as well.
>
> Adding methods to SFFloat doesn't seem to work.
>
> I'm pretty much bamboozled, and the next step is to replace many str()
> calls in x3d.py with my own function, which was my original idea.
>
> Another idea is to convert all floating point input to x3d.py to strings.
>
> Hmm! I don't like python much, as a beginner.
>
>
> On Thu, Oct 19, 2023 at 9:56 AM John Carlson <yottzumm at gmail.com> wrote:
>
>> I’m guessing I can override float.__str__() and maybe the list.__str__()
>> methods. Do I do that in x3d.py or my code?
>>
>> I’m on unfamiliar ground and will have to do some testing unless someone
>> has a definitive response.
>>
>> I hope this works. I will run through the smoke tests provided.
>>
>> I will probably modify the Blender X3DV exporter first, and if that
>> doesn’t work, proceed to modify x3d.py. I don’t think it’s a good idea to
>> modify x3d.py itself.
>>
>> So far, I haven’t seen anyone replacing the str() function.
>>
>> John
>>
>>
>> On Mon, Oct 16, 2023 at 11:10 AM GPU Group <gpugroup at gmail.com> wrote:
>>
>>> It seems possible to override functions in python, if there are just a
>>> few to override.
>>> And one suggestion for x3d.py imperfections is to apply fixes at runtime
>>> in some initialization function.
>>> - that assumes/requires just a few target functions, not 480 functions
>>> that need to be repaired
>>> - so for some fixes it may be helpful to change the upstream x3d.py to
>>> call a function that can be easily replaced.
>>> -Doug
>>> Example:
>>> file myclass.py
>>> class Wonky:
>>> def __init__(self):
>>> self.color = "Red"
>>> def func1(self, color :str) -> bool:
>>> if color == self.color:
>>> return True
>>> return False
>>>
>>> file override.py
>>> from myclass import *
>>>
>>> mc = Wonky()
>>> print("match with Blue? "+str(mc.func1("Blue")))
>>>
>>> def func2(self, color: str) -> bool:
>>> if self.color != color:
>>> return True
>>> return False
>>> Wonky.func1 = func2 # <<<<<<<<<<<< OVERRIDE
>>> print("don't match with Blue? "+str(mc.func1("Blue")))
>>>
>>> output:
>>> match with Blue? False
>>> don't match with Blue? True
>>> -Doug
>>>
>>> On Mon, Oct 16, 2023 at 9:55 AM John Carlson <yottzumm at gmail.com> wrote:
>>>
>>>> Instead of calling str() to print out a value, call a function that
>>>> does precision floating point formatting on floats according to a user’s
>>>> wishes. If the value is a list, call the function recursively with a list
>>>> comprehension, otherwise, call str.
>>>>
>>>> I think this is doable by mere humans.
>>>>
>>>> Thanks,
>>>>
>>>> John
>>>>
>>>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://web3d.org/pipermail/x3d-public_web3d.org/attachments/20231019/8c2c24fa/attachment.html>
More information about the x3d-public
mailing list