I want to remove a few ads in one server request.
What is the difference between:
batch-processing (only async?)
bulk mutates (only sync, shorter code?)
I have tried both ways and got errors:
batch-processing
I've tried to follow this post about batch-processing to create an async batch job for removing multiple ads. It was sent to the server, but I didn't see the sent ad ids were deleted.
Do I miss anything?
class ServiceWrapper:
"""Wraps GoogleAdsService API request"""
# public properties ...
def __init__(self, client, customer_id):
self._client = client
self._ga_service = client.get_service("GoogleAdsService")
self._ad_group_ad_service = client.get_service("AdGroupAdService")
self._batch_job_service = client.get_service("BatchJobService")
self._customer_id = customer_id
self._batch_job_operation = self._create_batch_job_operation(client)
self._batch_job_resource_name = self._create_batch_job(self._batch_job_service, customer_id,
self._batch_job_operation)
def _create_batch_job_operation(self, client):
"""Created a BatchJobOperation and sets an empty BatchJob instance to
the "create" property in order to tell the Google Ads API that we're
creating a new BatchJob.
Args:
client: an initialized GoogleAdsClient instance.
Returns: a BatchJobOperation with a BatchJob instance set in the "create"
property.
"""
batch_job_operation = client.get_type("BatchJobOperation")
batch_job = client.get_type("BatchJob")
client.copy_from(batch_job_operation.create, batch_job)
return batch_job_operation
def _create_batch_job(self, batch_job_service, customer_id, batch_job_operation):
"""Creates a batch job for the specified customer ID.
Args:
batch_job_service: an instance of the BatchJobService message class.
customer_id: a str of a customer ID.
batch_job_operation: a BatchJobOperation instance set to "create"
Returns: a str of a resource name for a batch job.
"""
try:
response = batch_job_service.mutate_batch_job(
customer_id=customer_id, operation=batch_job_operation
)
resource_name = response.result.resource_name
print(f'Created a batch job with resource name "{resource_name}"')
return resource_name
except GoogleAdsException as exception:
handle_googleads_exception(exception)
def add_all_batch_job_operations(self, batch_job_service, operations, resource_name):
"""Adds all mutate operations to the batch job.
As this is the first time for this batch job, we pass null as a sequence
token. The response will contain the next sequence token that we can use
to upload more operations in the future.
Args:
batch_job_service: an instance of the BatchJobService message class.
operations: a list of a mutate operations.
resource_name: a str of a resource name for a batch job.
"""
try:
response = batch_job_service.add_batch_job_operations(
resource_name=resource_name,
sequence_token=None,
mutate_operations=operations,
)
print(
f"{response.total_operations} mutate operations have been "
"added so far."
)
# You can use this next sequence token for calling
# add_batch_job_operations() next time.
print(
"Next sequence token for adding next operations is "
f"{response.next_sequence_token}"
)
except GoogleAdsException as exception:
handle_googleads_exception(exception)
def remove_disapproved_ads_for_account(account):
"""Remove all disapproved ads for a given customer id"""
ad_removal_operations = []
for row in rows:
ad_removal_operations.append(
build_removal_operation(customer_id, ad_json["ad_group_id"],
if len(ad_removal_operations) > 0:
remove_ads(ad_removal_operations)
#serviceWrapper.mutate(customer_id, [mutate_operation1, mutate_operation2])
def build_removal_operation(customer_id, ad_group_id, ad_id):
"""Removes the specified ad"""
resource_name = serviceWrapper.ad_group_ad_service.ad_group_ad_path(
customer_id, ad_group_id, ad_id
)
ad_group_ad_operation = serviceWrapper.client.get_type("AdGroupAdOperation")
ad_group_ad_operation.remove = resource_name
return ad_group_ad_operation
async def remove_ads(removal_operations):
"""Removes the specified ad"""
serviceWrapper.add_all_batch_job_operations(serviceWrapper.batch_job_service, removal_operations,
serviceWrapper.batch_job_resource_name)
operations_response = _run_batch_job(serviceWrapper.batch_job_service, serviceWrapper.batch_job_resource_name)
# Create an asyncio.Event instance to control execution during the
# asyncronous steps in _poll_batch_job. Note that this is not important
# for polling asyncronously, it simply helps with execution control so we
# can run _fetch_and_print_results after the asyncronous operations have
# completed.
_done_event = asyncio.Event()
_poll_batch_job(operations_response, _done_event)
# Execution will stop here and wait for the asyncronous steps in
# _poll_batch_job to complete before proceeding.
await _done_event.wait()
_fetch_and_print_results(serviceWrapper.client, serviceWrapper.batch_job_service,
serviceWrapper.batch_job_resource_name)
def _run_batch_job(batch_job_service, resource_name):
"""Runs the batch job for executing all uploaded mutate operations.
Args:
batch_job_service: an instance of the BatchJobService message class.
resource_name: a str of a resource name for a batch job.
Returns: a google.api_core.operation.Operation instance.
"""
try:
response = batch_job_service.run_batch_job(resource_name=resource_name)
print(
f'Batch job with resource name "{resource_name}" has been '
"executed."
)
return response
except GoogleAdsException as exception:
handle_googleads_exception(exception)
def _poll_batch_job(operations_response, event):
"""Polls the server until the batch job execution finishes.
Sets the initial poll delay time and the total time to wait before time-out.
Args:
operations_response: a google.api_core.operation.Operation instance.
event: an instance of asyncio.Event to invoke once the operations have
completed, alerting the awaiting calling code that it can proceed.
"""
loop = asyncio.get_event_loop()
def _done_callback(future):
# The operations_response object will call callbacks from a daemon
# thread so we must use a threadsafe method of setting the event here
# otherwise it will not trigger the awaiting code.
loop.call_soon_threadsafe(event.set)
# operations_response represents a Long-Running Operation or LRO. The class
# provides an interface for polling the API to check when the operation is
# complete. Below we use the asynchronous interface, but there's also a
# synchronous interface that uses the Operation.result method.
# See: https://googleapis.dev/python/google-api-core/latest/operation.html
operations_response.add_done_callback(_done_callback)
def _fetch_and_print_results(client, batch_job_service, resource_name):
"""Prints all the results from running the batch job.
Args:
client: an initialized GoogleAdsClient instance.
batch_job_service: an instance of the BatchJobService message class.
resource_name: a str of a resource name for a batch job.
"""
print(
f'Batch job with resource name "{resource_name}" has finished. '
"Now, printing its results..."
)
list_results_request = client.get_type("ListBatchJobResultsRequest")
list_results_request.resource_name = resource_name
list_results_request.page_size = BULK_REMOVE_PAGE_SIZE
# Gets all the results from running batch job and prints their information.
batch_job_results = batch_job_service.list_batch_job_results(
request=list_results_request
)
for batch_job_result in batch_job_results:
status = batch_job_result.status.message
status = status if status else "N/A"
result = batch_job_result.mutate_operation_response
result = result or "N/A"
print(
f"Batch job #{batch_job_result.operation_index} "
f'has a status "{status}" and response type "{result}"'
)
# [END add_complete_campaigns_using_batch_job_4]
Bulk Mutates
If I choose to follow this post about Bulk Mutates, and create a sync batch, I get an undefined symbol :Mutate how can I fix this? Or make this code work?
class ServiceWrapper:
"""Wraps GoogleAdsService API request"""
# public properties ...
def __init__(self, client, customer_id):
self._client = client
self._ga_service = client.get_service("GoogleAdsService")
self._ad_group_ad_service = client.get_service("AdGroupAdService")
self._batch_job_service = client.get_service("BatchJobService")
self._customer_id = customer_id
self._batch_job_operation = self._create_batch_job_operation(client)
self._batch_job_resource_name = self._create_batch_job(self._batch_job_service, customer_id,
self._batch_job_operation)
def build_removal_operation_sync(customer_id, ad_group_id, ad_id):
mutate_operation1 = serviceWrapper.client.operation(:Mutate)
"""Removes the specified ad"""
resource_name = serviceWrapper.ad_group_ad_service.ad_group_ad_path(
customer_id, ad_group_id, ad_id
)
ad_group_ad_operation = serviceWrapper.client.get_type("AdGroupAdOperation")
ad_group_ad_operation.remove = resource_name
mutate_operation1.ad_group_ad_operation = campaign_operation
Related
`timescale 1ns / 1ps
module DataMemory(
input [31:0] Address,
input [31:0] Writedata,
input MemRead,
input MemWrite,
output [31:0] ReadData
);
reg [0:31] mem[15:0];
mem[0] = 32'haaaaaaaa;
mem[1] = 32'haaaaaaaa;
mem[2] = 32'haaaaaaaa;
mem[3] = 32'haaaaaaaa;
mem[4] = 32'haaaaaaaa;
mem[5] = 32'haaaaaaaa;
mem[6] = 32'haaaaaaaa;
mem[7] = 32'haaaaaaaa;
mem[8] = 32'haaaaaaaa;
mem[9] = 32'haaaaaaaa;
mem[10] = 32'haaaaaaaa;
mem[11] = 32'haaaaaaaa;
mem[12] = 32'haaaaaaaa;
mem[13] = 32'haaaaaaaa;
mem[14] = 32'haaaaaaaa;
mem[15] = 32'haaaaaaaa;
begin
process(MemWrite, MemRead) // pulse on write
begin
if (MemWrite = '1' ) then
DM( ( to_integer(unsigned(Address)) - 268500992)/4 <= WriteData;
end if;
if ( MemRead = '1') then
ReadData <= DM( ( to_integer(unsigned(Address)) - 268500992 ) /4 );
end if;
I am debugging this code and cannot figure out why the lines do not work:
reg [0:31] mem[15:0];
mem[0] = 32'haaaaaaaa;
mem[1] = 32'haaaaaaaa;
mem[2] = 32'haaaaaaaa;
mem[3] = 32'haaaaaaaa;
mem[4] = 32'haaaaaaaa;
mem[5] = 32'haaaaaaaa;
mem[6] = 32'haaaaaaaa;
mem[7] = 32'haaaaaaaa;
mem[8] = 32'haaaaaaaa;
mem[9] = 32'haaaaaaaa;
mem[10] = 32'haaaaaaaa;
mem[11] = 32'haaaaaaaa;
mem[12] = 32'haaaaaaaa;
mem[13] = 32'haaaaaaaa;
mem[14] = 32'haaaaaaaa;
mem[15] = 32'haaaaaaaa;
Thank you in advance.
I am going to guess that you are trying to initialize the array. You can do that using this syntax:
reg [0:31] mem[15:0] = '{
32'haaaaaaaa,
32'haaaaaaaa,
32'haaaaaaaa,
32'haaaaaaaa,
32'haaaaaaaa,
32'haaaaaaaa,
32'haaaaaaaa,
32'haaaaaaaa,
32'haaaaaaaa,
32'haaaaaaaa,
32'haaaaaaaa,
32'haaaaaaaa,
32'haaaaaaaa,
32'haaaaaaaa,
32'haaaaaaaa,
32'haaaaaaaa};
I am trying to write a function in Python3 GUI where
User inputs three different readings and clicks 'continue'
Each reading needs to be a positive float/int
If the above format is not received an error message is displayed in a label "text"
once the readings are valid my function is to calculate the total of each reading and display the largest value on the GUI.
This is my code but I'm not sure how to return the values of each reading and return the highest value using another function.
def num_o(): try:
O = float(o_entry.get())
T = (50 * O)/6
Ta = ("%0.2f" % (T))
answer["text"]= 'Total:', str(Ta)
assert O > 0
except AssertionError:
answer.config(text="Invalid Entry")
except ValueError:
answer.config(text="Invalid Entry")
return answer
def num_s():
try:
S = float(s_entry.get())
T1 = (20 * S)/2
Tb = ("%0.2f" % (T1))
answer["text"]= 'Total:', str(Tb)
assert S > 0
except AssertionError:
answer.config(text="Invalid Entry")
except ValueError:
answer.config(text="Invalid Entry")
return answer
def num_p():
try:
P = float(p_entry.get())
T2 = (25 * P)/3
Tc = ("%0.2f" % (T2))
answer["text"]= 'Total:', str(Tc)
assert P > 0
except AssertionError:
answer.config(text="Invalid Entry")
except ValueError:
answer.config(text="Invalid Entry")
return answer
answer["text"]= 'Total:', str(Ta + Tb + Tc) # not sure how to return the highest value and display as a label
I have a tables like this
Table 1
Date ID Category Product
24-July-2018 1 A Product1
24-July-2018 2 A Product2
25-July-2018 2 A Product2
24-July-2018 3 B Product3
24-July-2018 4 B Product3
25-July-2018 5 C Product2
24-July-2018 1 D Product1
24-July-2018 2 D Product2
25-July-2018 2 D Product2
24-July-2018 3 E Product3
24-July-2018 4 E Product3
25-July-2018 5 E Product2
24-July-2018 1 F Product1
24-July-2018 2 F Product2
25-July-2018 2 G Product2
24-July-2018 3 H Product3
24-July-2018 4 I Product3
25-July-2018 5 J Product2
I want to display top products for each category for top 6 categories by product count for that date(The date column is in filter). For example, if the user selects 24-July-2018 from filter, the results should be
SubTable1 (B)
Id Product Count
1 Product3 2
SubTable2 (D)
Id Product Count
1 Product1 1
2 Product3 1
SubTable3 (E)
Id Product Count
1 Product3 2
Subtable4 (F)
Id Product Count
1 Product1 1
2 Product2 1
Subtable5 (H)
Id Product Count
1 Product3 1
Subtable6 (A)
Id Product Count
1 Product2 1
and if the filter is 25-July-2018, the results should be -
SubTable1 (A)
Id Product Count
1 Product2 1
SubTable2 (C)
Id Product Count
1 Product1 1
SubTable3 (D)
Id Product Count
1 Product2 1
Subtable4 (E)
Id Product Count
1 Product1 1
Subtable5 (G)
Id Product Count
1 Product2 1
Subtable6 (J)
Id Product Count
1 Product1 1
Does this work for you??
If yes you simply need to group by Category and Date. Let me know if this helps
import RPi_I2C_LCD
from time import *
import serial
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(19,GPIO.OUT)
GPIO.setwarnings(False)
lcd = RPi_I2C_LCD.LCD()
lcd.set_backlight(True)
lcd.set_cursor(row=0)
lcd.message("DINESH SIR")
lcd.set_backlight(True)
ser=serial.Serial(port='/dev/tty1',baudrate=9600,parity=serial.PARITY_NONE,timeout=1)
while(True):
x=ser.read()
print(x)
if x=="1" or x=="b":
GPIO.output(19,True)
print("on")
lcd.message("ON")
else:
GPIO.output(19,False)
print("off")
lcd.set_cursor(row=1)
lcd.message("OFF")
When I'm trying to connect hc-05 bluetooth module with my Raspberry Pi running Raspbian streach OS, then using the above code, I'm getting the output as follows
b ''
off
b ''
off
b''
off
b''
off
b''
off
b''
off
b''
off
b''
off
b''
and so on executing in a loop.
This is a slightly different take on a previous question I asked. I tried to modify the great advice I received but couldn't quite get there.
I am looking to find text in a cell (not case specific), then return the preceding word plus the found text. I prefer a formulaic solution (over a macro).
Sample cell contents include the following where I'm looking for "TRUCK":
"This is a test dump TRUCK like it 54" - I want "dump TRUCK"
"This is pick-up Truck forgot 346 I like" - I want "pick-up Truck"
I tried some combo of the Mid() function but wasn't quite getting it:
=MID(B2,(FIND("Truck",B2,1)-1),FIND(" ",B2,FIND("Truck",B2,1)-1)-FIND("Truck",B2,1)+1)
Thanks for the help!
=TRIM(RIGHT(SUBSTITUTE(LEFT(B2,SEARCH(" Truck ",B2)+5)," ",REPT(" ",99)),199))
LEFT(B2,SEARCH(" Truck ",B2)+5) gets the "This is a test dump TRUCK" part.
Then SUBSTITUTE( ^ ," ",REPT(" ",99)) gets:
This is a test dump TRUCK
and =TRIM(RIGHT( ^ ,199)) gets the last 2 words from the above and removes the extra spaces.