

david_navigator
-
Content Count
153 -
Joined
-
Last visited
Posts posted by david_navigator
-
-
Something I found it really useful is explaining code that I don't understand.
For example last week someone sent me a function in Python (a new language for me) which even with the comments I couldn't work out what it was doing.
I asked ChatGPT to explain the code, and it did, in a way that then meant I could code something in delphi very quickly.
For example, with your code.
QuoteThis code defines a Delphi unit called "Person", which contains the definition of a class called "TPerson". This class represents a person and has three properties: FirstName, LastName, and Age. The properties are implemented with getter and setter methods that read and write private fields.
The class also has two methods that are used to convert the person object to and from JSON format. The ToJSON method takes the person object and returns a JSON string that represents the object's properties. The FromJSON method takes a JSON string and creates a new person object with properties that match the values in the JSON string.
The implementation of the ToJSON method creates a new TJSONObject and adds the person's properties to it as name-value pairs. The method then returns the JSON string representation of the TJSONObject.
The implementation of the FromJSON method first parses the JSON string into a TJSONObject, and then creates a new TPerson object. The method then sets the properties of the new TPerson object to the values of the corresponding name-value pairs in the TJSONObject.
The "uses" clause at the top of the unit indicates that the System.SysUtils and System.JSON units are required for this code to compile.
-
why didn't I think of asking #AI ??
can you explain what this python code does ? def marsaglia_xorshift_128(x = 123456789, y = 362436069, z = 521288629, w = 88675123): while True: t = (x ^ (x << 11)) & 0xffffffff x, y, z = y, z, w w = (w ^ (w >> 19) ^ (t ^ (t >> 8))) yield w This is a Python function that implements the Marsaglia Xorshift 128 random number generator algorithm. The function takes four parameters x, y, z, and w which serve as the seed values for the generator. The function contains an infinite loop that generates random numbers using the Xorshift 128 algorithm. The loop uses bitwise operations to perform the necessary calculations to generate a new random number on each iteration. The t variable is calculated by performing a bitwise XOR operation on x shifted left by 11 bits and x, and then taking the result and performing a bitwise AND with the hexadecimal value 0xffffffff. The purpose of this calculation is to produce a new value for t that is used in the calculation of the next random number. Next, x, y, and z are updated by taking the previous values of y, z, and w. w is then calculated by performing a series of bitwise XOR and right-shift operations. The yield statement returns the new value of w on each iteration of the loop. This allows the function to be used as a generator that can be used to produce an infinite sequence of random numbers. Each time the function is called with the same seed values, it will produce the same sequence of random numbers. To get a different sequence, the seed values must be changed.
-
1
-
-
I need to convert some python code to delphi, though my knowledge of python is somewhat thin. I've done most of what's needed but there's a part that I don't really understand.
starting with _extract, it seems to call marsaglia_xorshift_128 but why does that have a while True & yield - is that so everything is treated as a persistent variable ?
Does marsaglia_xorshift_128 effectively get called again for each byte in contents[12:16], from the deobfuscate_string so that w is the value from the "next" iteration ?How would I do something similar in delphi ?
def marsaglia_xorshift_128(x = 123456789, y = 362436069, z = 521288629, w = 88675123): while True: t = (x ^ (x << 11)) & 0xffffffff x, y, z = y, z, w w = (w ^ (w >> 19) ^ (t ^ (t >> 8))) yield w # The lower 8-bits from the Xorshift PNR are subtracted from byte # values during extraction, and added to byte values on insertion. # When calling deobfuscate_string() the whole string is processed. def deobfuscate_string(pnr, obfuscated, operation=int.__sub__): return ''.join([chr((operation(ord(c), pnr.next())) & 0xff) for c in obfuscated]) def _extract(container_filename): ... ... ... contents = container.read(compressed_length) header_length, mangling_method, truncated_timestamp, original_length = struct.unpack('>HHLL', contents[:12]) assert header_length == 12 and mangling_method == 1 # The file contents are obfuscated with a Marsaglia xorshift PNR pnr = marsaglia_xorshift_128(x = truncated_timestamp, y = original_length) qcompress_prefix = deobfuscate_string(pnr, contents[12:16])
-
2 hours ago, Stano said:I know/I read of two cases where programmers used it. AI reduced their time to create the right code from a week to a day. No, they didn't get the right result. But AI led them to it.
He who knows what he wants and what he is doing will help himself.
That's the experience I've had. Each sample of code has not compiled out of the box, but fixing it has given me an understanding of the code or pointed me in the direction of something I didn't know about.
-
1
-
-
Thanks. I did a search but nothing came up - maybe the search needs more AI 🙂
-
Interestingly none of the code samples it's given me so far have worked "out of the box" & many have contained deprecated or obsolete units. From what I can see, looking at the ExpertsExchange data it seems to have used, it weighs older information higher than recent information, which is of course not what's wanted with delphi.
-
-
-
2 hours ago, Vandrovnik said:And what happens when you close the Local Variables window?
exactly the same, but without the exception name being displayed. i.e delphi still grabs the focus for a few seconds before returning it to my app.
-
No one any suggestions - delphi has become impossible to debug with this unit as every click in my app now is followed by 20 seconds of flashing debugger.
-
you seem to be setting buf_size to 0.
What happens if you use
buf_size[0] := $FF; buf_size[1] := $FF; buf_size[2] := $FF; buf_size[3] := $FF;
-
In my app, when I do anything that interacts with a specific control, the IDE grabs the focus and repeatedly displays KERNELBASE.RaiseException + 0x62 in the Local Variables Process selector.
Not seen this before.Should I be concerned ? Doesn't seem to have any affect when running the exe outside of Delphi.
This video demonstrates what I mean https://navigator.tinytake.com/msc/NzQ5MDQxM18yMDQxNTMxMQ -
I had a similar problem recently and it turned out to be due to a third party control on another form within my project.
The 3rd party control did something in it's constructor that resulted in not only message dialogs opening behind, but popups, such as calendars doing similar - it of course didn't happen in normal use, but just when I happened to have two unrelated 3rd party controls in my app. (Thinfinity & TMS Planner) -
Problem solved.
If the SendMessage originates from a different thread to that Presenting the Notification, then Windows raises the error "An outgoing call cannot be made since the application is dispatching an input synchronous call."
As this is raised in the constructor of TNotificationWinRT, the destructor is called. The destructure doesn't check if FToast is assigned (which it's not 'cos the constructor fails) and thus raises an Access violation. I'll report this via QC.
Meanwhile the fix to the original problem seems to be as simple as introducing a timer (which I'm guessing moves the processing to the main thread - is there a better way to do this ?).
So the code becomes something like this (from my test app)
procedure TForm62.comportclient1Receive(Sender: TObject; InQue: Integer); begin SendMessage(handle, pm_ProcessBarcodeScan, WPARAM(0), LPARAM(0)); application.processmessages; end; procedure TForm62.ProcessBarcodeScan(var Message: TMessage); begin inherited; SendWin10Notification('Hello World', 'Sent by Message'); end; procedure TForm62.SendWin10Notification(aTitle, aAlertBody: string); begin FNotification.Name := format('Windows10Notification%d', [GetTickcount]); FNotification.Title := aTitle; FNotification.AlertBody := aAlertBody; if not insendmessage then NotificationCenter1.PresentNotification(FNotification) else Timer1.enabled := true; end; procedure TForm62.Timer1Timer(Sender: TObject); begin if not insendmessage then begin NotificationCenter1.PresentNotification(FNotification); timer1.enabled := false; end; end;
-
QuoteCheck TNotificationWinRT.Create.
I assume your lNotification is freed before it used here.
I think it must be something like that.
Interestingly if I change the code so that rather than a local variable (which might go out of scope ?) the Notification is a class field, I get an additional error "An outgoing call cannot be made since the application is dispatching an input-synchronous call", then I get an AV !!
-
Yes, Debug DCU's is checked.
I can step through a lot of the source, until I get to
Result := TToastNotificationManager.Statics.GetTemplateContent(LTemplateType);
where the debugged just goes in to some interface assembler and no further.
QuoteAnyway, the use of Application.ProcessMessages looks like a code flaw and is the wrong approach in most cases.
In this case, it's part of the design so that serial port data gets dealt with in the background whilst a dialog is visible
if ScansDismissPromptsCheckbox.Checked then PostMessage(handle, pm_ProcessBarcodeScan, WPARAM(lKeyCode), LPARAM(Ord(TRUE))) // see bug tracker 3503 , need to be a PostMessage else begin SendMessage(handle, pm_ProcessBarcodeScan, WPARAM(lKeyCode), LPARAM(Ord(TRUE))); // see bug tracker 3009 , need to be a SendMessage application.ProcessMessages; end;
-
I am getting an Access Violation in the VCL TNotificationCenter, however how it occurs is strange and I can't seem to debug far enough in to the delphi source to work out why.
I have a delphi form with an editbox.
The edit box has an OnKeyDown event.
Within that OnKeyDown event we call a routine to check if the key was VK_RETURN and if it was, we then process the content of the edit box (Method ProcessBarcodeScan) and in certain circumstances we create a Windows 10 (Toast) notification.
(The notification makes no reference to the edit box or any of it's properties.)
Now, the odd bit, if I type in to the edit box, then everything works as it should and there are no errors and the "toast" pops up.
If I populate the Edit box via a SendMessage thus
SendMessage(BarcodeEdit.Handle, WM_CHAR, lKeyCode, 0);
and then call the ProcessBarcodeScan method via a PostMessage, thus
PostMessage(handle, pm_ProcessBarcodeScan, WPARAM(lKeyCode), LPARAM(Ord(TRUE)))
again all works correctly.
However, if I populate the edit box via the SendMessage as above and then call ProcessBarcodeScan via a SendMessage thus
SendMessage(handle, pm_ProcessBarcodeScan, WPARAM(lKeyCode), LPARAM(Ord(TRUE))); application.ProcessMessages;
I get an Access violation, which stepping through the code gets me to
class function TToastTemplateGenerator.GetXMLDoc(const ANotification: TNotification): Xml_Dom_IXmlDocument; ... ... Result := TToastNotificationManager.Statics.GetTemplateContent(LTemplateType);
but the only place I can find GetTemplateContent is in Winapi.UI.Notifications class function TToastNotificationManager which won't accept a break point
This is my Notification code
procedure TDMNexusConnect.SendWin10Notification(aTitle, aAlertBody: string); var lNotification: TNotification; begin lNotification := NotificationCenter.CreateNotification; try lNotification.Name := format('Windows10Notification%d', [GetTickcount]); lNotification.Title := aTitle; lNotification.AlertBody := aAlertBody; NotificationCenter.PresentNotification(lNotification); finally lNotification.Free; end; end;
and this is the Access Violation.
exception class : EAccessViolation exception message : Access violation at address 03C4EBE9 in module 'HireTrackNX.exe'. Read of address 00000000. thread $2c24: 03c4ebe9 +015 HireTrackNX.exe System.Win.Notification TNotificationWinRT.Destroy 77e64ee1 +021 ntdll.dll KiUserExceptionDispatcher 00407984 +004 HireTrackNX.exe System 34 +0 @FreeMem 0040cc24 +01c HireTrackNX.exe System 34 +0 @UStrClr 03c4eaa6 +046 HireTrackNX.exe System.Win.Notification TNotificationWinRT.Create 03c4deb3 +02f HireTrackNX.exe System.Win.Notification TNotificationCenterWinRT.DoPresentNotification 03c59b40 +018 HireTrackNX.exe System.Notification TCustomNotificationCenter.DoPresentNotification 03c59c52 +002 HireTrackNX.exe System.Notification TCustomNotificationCenter.PresentNotification 03de9d23 +0a7 HireTrackNX.exe DMNexus 2440 +7 TDMNexusConnect.SendWin10Notification
QuoteQuote -
50 minutes ago, bobD said:four questions:
1. Where do the datetimes come from and how/when do they change? For example, if you're comparing file datetimes, then maintaining a status when the data changes might be better than checking when you need to know. They might change a lot less often than you check for a difference.
2. What is it you really need to know: whether any particular pair is different? or whether any difference exists anywhere in the list?
3. Is that always simply a 'difference exists' boolean? Or would knowing [greater than | equal to | less than] also needed?
4. Is there a guarantee that for every element[x] in list one, the same element exists at position[x] in list two? How is that guarantee enforced or checked?
All of these affect your potential solution options. For the first example, if the list changes much less often than the need to check, then you might create a tracking object with two boolean values: FUpdated and FDifferent. Set FUpdated to true when any list element changes. When checking, if FUpdated is false, then simply return the FDifferent boolean, or if true recheck the list, store and return the comparison operation result, and reset FUpdated to false. This can save you thousands of list comparisons if the lists are much less volatile than the need to know whether they're the same. Answers to the other questions could modify the structure of this tracking object.
1. My UI, so I "know" when a user changes a date.
2. I need to know if any lists are the same as my list.
3. answered by 2 - I don't want to know anything if they're different.
4. No. Imagine each list represents a hotel room and each element of the list is the check in and check out time for each visitor over the next 6 months - I need to know if any two rooms have exactly the same booking schedules.The dates change frequently, the comparison gets run infrequently, but the compare has to be very fast and the date changes can be slow.
-
25 minutes ago, Dany Marmur said:You say "sets", does that imply that the date-lists are unsorted?
The date lists are sorted.
There can be N date lists,
I need to know which date lists match the current one. -
38 minutes ago, joaodanet2018 said:I think that using the follow, will be more flexible;
-- aux: array, dictionary, list, record, class, etc...
<dateOne, dateTwo, boolean> = 25/02/2022, 24/02/2022, false == a "class" can allow more controll about setters and getters.
if dont need the date-values, then, store one "a boolean"
Thanks, for the idea, though I don't it won't work in this particular case as there are actually many lists and I need to know if any list matches my "current" list.
-
45 minutes ago, Uwe Raabe said:Simple approach: Create a string representation of each list and compare the strings when needed.
Of course you have to re-create the string whenever a list changes.
Crafty 🙂
-
I have a two sets of datetimes which I need to frequently check to see if the two lists are identical. There isn't the time to iterate through the lists whenever I need this info, so I'm thinking I need to store a value that represents the data.
I think it's a hash, but this isn't something I've done before and everything I've attempted hasn't worked i.e I've changed some of the datetimes and ended up with the same result.If it's relevant there are usually about 60 in each set, but it could be between two and a thousand (always an even number) and each is only stored to the minute i.e seconds & mS are always zero.
Can anyone point me in the correct direction please ?
Many thanks
David
-
23 minutes ago, KenR said:I don't think you'll find anything better than the DevEx one but you will have to buy all of their VCL components,
Luckily I already have the DexEx & TMS controls mentioned in this thread and I have the budget for others if something suitable raised its head 🙂
-
2 hours ago, Bill Meyer said:Since I do not know the answer myself, does Excel support the attached object? Or is that simply something you need in your UI?
If the latter, then you may find looking at TMS FlexCel useful. It is a very capable Excel interface I have used for several years. I think you will find you need a hybrid solution to meet your requirements.
Just my UI. The customer currently uses Google sheets as the UI and then copies data in to various other apps etc. They're after a similar UI, but with everything else automated. I've looked at various UI ideas and I too have come to the conclusion that for this particular task the excel feel is the best for the job. The solution doesn't need to be able to export in to excel or google sheets format and must be completely self contained i.e not just an automation of excel or sheets.
New blog post: Leveraging ChatGPT to generate a Delphi class along with CRUD code from a table schema
in Tips / Blogs / Tutorials / Videos
Posted
Me too and there's definitely a learning curve in working out what ChatGPT is good at and what it's not.
Tonight I got it to do, like you, the boring part of writing some class code (I was going to say creating, but I think I'm creating, it's just writing), but I also asked it to write a simple function to remove tags from some HTML to just leave plain text. Even after 8 iterations the code wouldn't still wouldn't compile and even after I'd fixed those issues, the code didn't do what was requested. I'm guessing too many StackOverflow wrong answers have been analysed as code sampled !