Running a Function in the background while asking for more inputs - function

I am creating a little data processing script using selenium. Where I input my values and it runs a function to do the task on a website. I would like to queue inputs so that I can enter the new values while it works on the old ones.
while customername != 1:
print("Customer name")
customername = input()
print("Credit amount")
creditamount = input()
addcredit(driver, customername, creditamount)
How would I get the function addcredit() to run while the loop continues and asks me for the next set of inputs?
Thank you all!

So after a bit more research, I used Threading.
p1 = threading.Thread(target=addcredit, args=(driver, customername, creditamount))
p1.start()
this is allowing my script to run as intended.. where it starts the action and then allows me to type more data in to run the action again. from my understanding when the function called in the second thread sleeps it bounces back to the first thread and continues on. someone, please correct me if I am wrong.

Related

awesomewm: a function to move all open clients to their designated screens/tags

Reading on the use of mod + x: run lua code I wanted to insert a function in my rc.lua that would move all opened clients (back) to their screens and tags. Everytime I take my laptop out of the docking the windows end up on one screen on the first tag, so If I could restore them running a function, that would save me a lot of time.
But...
I have no experience in LUA at all, so I tried this
shuffle = function(...)
clientlist = {}
for i, c in ipairs(client.get()) do
-- assign tag
-- c:tags (tag)
-- c:move_to_screen ([s=c.screen.index+1])
table.insert(clientlist,c.name)
naughty.notify{c.name}
end
naughty.notify{table.concat(clientlist,'\n')}
end
When I run this, I get a number of notifications, that respond to the number of open clients + 1, but there is nothing in them.
What am I doing wrong ?
Also, is there an easier way to debug this, instead of using the notify box ?
The answer to the question that you are actually asking is: You need naughty.notify{ title = c.name }.

Function 'DEQUEUE_EMRKPF' doesn't dequeue

I want to dequeue a reservation in the db-table RKPF.
Why is this?
Actually I want to change a purchase requsition and therefore I need to execute the 'BAPI_PR_CHANGE'. I execute it twice to avoid failures. First as a test run and secondly as normal without the testrun-flag.
My problem is the following: during the first run (test run) it enqueues my reservation and the bapi leaves without dequeues my reservation, so I wanted to dequeue it by myself. but I failed
Does anybody know the solution of my problem?
Here my code to dequeue the reservation:
CALL FUNCTION 'DEQUEUE_EMRKPF'
EXPORTING
MODE_RKPF = 'E'
MANDT = SY-MANDT
RSNUM = mv_rsnum
_SCOPE = '3'
_SYNCHRON = 'X'
_COLLECT = ' '
.
Before giving me a answer check out these things I checked out before:
I called the transaction SM12 and checked if my mv_rsnum is the same as in the table
I didn't found any sy-subrc in the bapi
I dequeued the banf which has this reservation number..
Sorry guys. The Project must go on, therefore I deleted the code where I call the testrun of 'BAPI_PR_CHANGE'.
This BAPI locked the table RKPF and at the end, it doesn't unlock the RKPF table.
But THANKS TO ALL, who wanted to help me!

Lazarus, can't get DB Update working. How can I preview whats being submitted?

Hi I have a form setup using TMySQL51Connection, TSQLTransaction & TSQLQuery modules, it retrieves info from the DB without issue but I'm having problems with updates.
The DB is pretty large so I'm starting with just the 1st couple of rows, once they're working I'll extend the query.
My TSQLQuery.UpdateSQL is as follows
UPDATE table SET
ContactFirst = :ContactFirst,
ContactSur = :ContactSur
WHERE AccountID = :AccountID
Then I have a button in my form, the onCLick event contains:
begin
accSelect.Edit;
accSelect.Post;
accSelect.ApplyUpdates;
dbTransaction.CommitRetaining;
sqlbl1.Caption := accSelect.UpdateSQL;
end;
Clicking the buttons does precisely nothing with the DB, it generates no error messages, just updates the caption on sqlbl1. Is there anyway to preview what Lazarus is sending to the DB with values included so I can track down whats going wrong?
I tried adding a logging event to the SQLConnection but couldn't work out how to get it generating logs.
From my experience, the Edit, Post and ApplyUpdates methods are used in conjunction with a TClientDataSet. In that case you would simply assign new values to the fields in the CDS (after navigating to the record you want to Edit) then Post those changes (no need for an Update SQL statement). Something like this...
MyCDS.Edit;
MyCDS.FieldByName('ContactFirst').Value := sContactFirstName;
MyCDS.FieldByName('ContactSur').Value := sContactSurname;
MyCDS.Post;
MyCDS.ApplyUpdates;
Alternatively, you could use a Query component, write the UPDATE SQL into the SQL property, and use ExecSQL or whatever method has been implemented for that component (there are many database access components that each have their own idiosyncrasies)
Here's some code off the top of my head. Please excuse me but I'm not familiar with Lazarus specifics - it's basically Delphi code.
MySQLQuery.SQL.Clear;
MySQLQuery.SQL := 'UPDATE MyTable
SET ContactFirst = :ContactFirst,
ContactSur = :ContactSur
WHERE AccountID = :AccountID';
MySQLQuery.Params.ParamByName('ContactFirst').Value := sContactFirstName;
MySQLQuery.Params.ParamByName('ContactSur').Value := sContactSurname;
MySQLQuery.Params.ParamByName('AccountID').Value := iAccountID;
try
MySQLQuery.ExecSQL;
ShowMessage('Update succeeded');
except on e: Exception do
ShowMessage(e.Message);
end;
So it might be that you've ended up using half of both methods and neither of them completely.

MS Access 2007 - controlling UI behaviour after attempt to insert duplicate record

Creating a simple UI using MS Access, hoping to do minimal actual coding (actually helping a friend who is not a coder).
Simplified requirement: Single table, primary key is phone number, lots of other non-mandatory fields. Display a form allowing just the phone number to be entered, if a record with that key exists display the full record, if a record with that key does not exist bring up an form allowing the other fields to be entered for this phone number and hence create a new record.
Q1: Any simple way to achieve this kind of function? Example?
We've got some of this going with a standard form, can execute code if insertion fails, but a standard dialogue box is displayed warning about the duplciate key violation.
Q2: How can we trap that attempted insertion, avoid having the dialogue come up?
You will have to get your hands dirty and write some code to get this outcome. A starting point would be something like this presto code. Post back if you get stuck on any of the parts.
If fCheckIfRecordExists(lYourKey)=True then
Docmd.OpenForm “frmEditExistingRecord”
Else
Docmd.OpenForm “frmEnterNewRecord”
End if
Public function fCheckIfRecordExists (lYourKey as Long) as Boolean
‘Code to check if a record exists, simple method is to use dLookup or a count SQL statement with the criteria as the key you are trying to find
End function
EDIT:
First things first make a form with 1 text box called txtPhone_number and a command button called cmdSearch.
Next put this bit of code in the module behind the form
Public Function fDoes_record_exist(strPhone_number As String) As Boolean
If DCount("Phone_number", "tblYour_table", "Phone_number=" & strPhone_number) > 0 Then
fDoes_record_exist = True
Else
fDoes_record_exist = False
End If
End Function
Next you need to put some code behind the click event of the command button. This code can be expanded on to check for a valid phone number later if you want
If fDoes_record_exist(Me.txtPhone_number) = True Then
DoCmd.OpenForm "frmShow_existing_record"
Else
DoCmd.OpenForm "frmEnter_new_record"
End If
That should set you on your way nicely but post back if you run into problems
Here is an overview of the process with Access logic:
You need an unboud control labelled Phone in the form header, where user will be able to enter the phone number to search. You need to use the After_Update event of that control to trigger your search. There will be a second Phone control, bound this time, in the Detail section of the form for effective data entry/update.
Use the Form_Error event to intercept the error message when user tries to save a duplicate key, in order to display a nice message, and eventually Cancel his changes.
The advice from Kevin Ross to use VB Code is clearly one approach, and I think is appropropriate if we anticipate less trivial requirements in future. However I'm in a situation where I'm helping someone with zero coding background and hence if possible I'd prefer to let them use simple Macros rather than full-scale VB.
As it happens the functionality I require can be implemented with just Macros, and it depends on the suggestion from iDevelop.
The outline of the solution I used:
Create an InitialEntry form with no association to any particular table, it has:
a data entry field for the telephone number
a read-only text box where I can display a message
a button labelled Add
a button labelled Show
I write three macros:
A macro AlreadyExists that displays a message saying "We already that one"
A macro NewEntry that opens a data entry form for my table, in Add mode, and which copies the phone number from InitialEntry!TelephoneNumber
A macro TestForExisting this uses a condition
DCount("*","MyTable","[PhoneNumber] = [FormPhoneNumber] " ) > 0
to control whether to execute AlreadyExists, and a similar test to control whether to call NewEntry.
While this is not as efficient as VB, it does seem to be understandable by a non-coder, so at least we can implement our application.

How to insert a Please Wait form while a query is running

I want to display a "Please Wait" form while a 60+ second query is running. I have the query and the form made (easy enough), except the macro won't bring them together. Here is my autoexec macro:
SetWarnings=False
Open Wait Form
Run the query & Display Results
Close Wait Form
The problem is that I can only see the outline of my wait form while the maketable query is running. I can only guess that the macro doesn't wait for the form to completely load before going to the next step.
And just for fun:
The Wait form has a Timer that shows the number of seconds elapsed. How do I run the query while still allowing the form to update itself at the same time?
-------Update----------
Repaint only updates the background color before moving on to the query. I also tried 6 repaints in a row with no difference (I thought that might get around the bug you people mentioned). The form is very simple. A single text box that uses "loading" for the control source and a timer function (disabled for the moment). Here is my macro in slightly greater detail:
SetWarnings=False
Open Wait Form (modal=yes, popup=no)
Repaint the form
Open a maketable query (this query saves alot of time)
Close Wait Form
Open the display query
Firstly, you just need to Repaint the wait form after it is shown for it to show correctly, it's just a bug in VBA that sometimes causes forms not to be displayed properly even if they are drawn at the correct point in the code.
Secondly, if you set the wait form's ShowModal property to false then you will be able to update it through your routine. I use a public function that takes a 0-100 argument to update a progress bar. This works best in a loop where you can normally calculate the number of loops remaining and provide an accurate guide to the elapsed progress, but even when performing a series of operations you can time and pass suitable values through to the progress form so that the user is kept informed. This works better than most Windows progress bars!
The first problem is easy to solve, try putting me.Repaint before the query runs, sometimes access can get a bit lazy with screen painting and you have to force it.
As for the other thing that is a bit more complicated. You would have to execute the query asynchronously. You can do this in ADO by adding the adAsyncExecute option when executing the query. You would then catch the finishing event to tell the users the query is done
Here is a link on the Microsoft knowledge base http://support.microsoft.com/kb/262311
If you are using an Action Query you can track the number of executed rows by using a vba-function which returns the same value that was passed to it. Of course you have to know the total number of rows, to know when the query is done or display progress (This can often be achieved relatively fast using a lightweight counting query*).
'first some global vars to keep track of the number
Dim lngCurrentRow as Long
Dim lngTotalRows as Long 'We assume lngTotalRows have been set elsewhere
'Here is the function
Public Function UpdateProgress(FieldValue as Variant) as Variant
lngCurrentRow = Nz(lngCurrentRow, 0)
If (lngCurrentRow >= lngTotalRows) then
MsgBox "Query Done!"
Else
'Do stuff to show progress
End If
lngCurrentRow = lngCurrentRow + 1
End Function
For displaying the progress I have used an Unbound form, with an empty textfield with transparent background (progressFrame) and a label (progressLabel) which has a blue background and is placed behind the textbox to the left. For each call to UpdateProgress you can then set the width of the label:
progressLabel.Width = progressFrame.Width * (lngCurrentRow / lngTotalRows)
*: F.ex. if the query is based on one table, with some added data, so that the number of records the query will produce is equal to that of the table, a count can be done on the table alone so that it will be faster..
I hope this was helpful, or at least inspiring :)
I had the same issue with a pop up form which took a few seconds before it displayed the text which informs the user that something is processing so please wait. I added the line of code below after the code which opens the form and before the next bit of code which runs the update queries (which takes a while hence the need to inform the user that something was processing)
DoCmd.RepaintObject acForm, "frmMyForm"
Works perfectly for me.