How do I build a static library in WAF? - c++

My scenario goes like this:
wscript:
def options(opt):
opt.load('compiler_c')
opt.load('compiler_cxx')
def configure(ctx):
ctx.load('compiler_c')
ctx.load('compiler_cxx')
def build(ctx):
ctx.objects(source = "file0.c",
target = "cFiles")
ctx.objects(source = "file1.cpp file2.cpp",
target = "cxxFiles")
ctx.stlib(source = "???",
target = "test")
How do I call 'ctx.stlib()'?

ctx(features='c cxx cxxstlib', use='cFiles cxxFiles', target='test')
EDIT: removed dead link.

Related

Batch Prediction Job non-blocking

I am running a Vertex AI batch prediction using the python API.
The function I am using is from the google cloud docs:
def create_batch_prediction_job_dedicated_resources_sample(
key_path,
project: str,
location: str,
model_display_name: str,
job_display_name: str,
gcs_source: Union[str, Sequence[str]],
gcs_destination: str,
machine_type: str = "n1-standard-2",
sync: bool = True,
):
credentials = service_account.Credentials.from_service_account_file(
key_path)
# Initilaize an aiplatfrom object
aiplatform.init(project=project, location=location, credentials=credentials)
# Get a list of Models by Model name
models = aiplatform.Model.list(filter=f'display_name="{model_display_name}"')
model_resource_name = models[0].resource_name
# Get the model
my_model = aiplatform.Model(model_resource_name)
batch_prediction_job = my_model.batch_predict(
job_display_name=job_display_name,
gcs_source=gcs_source,
gcs_destination_prefix=gcs_destination,
machine_type=machine_type,
sync=sync,
)
#batch_prediction_job.wait_for_resource_creation()
batch_prediction_job.wait()
print(batch_prediction_job.display_name)
print(batch_prediction_job.resource_name)
print(batch_prediction_job.state)
return batch_prediction_job
datetime_today = datetime.datetime.now()
model_display_name = 'test_model'
key_path = 'vertex_key.json'
project = 'my_project'
location = 'asia-south1'
job_display_name = 'batch_prediction_' + str(datetime_today)
model_name = '1234'
gcs_source = 'gs://my_bucket/Cleaned_Data/user_item_pairs.jsonl'
gcs_destination = 'gs://my_bucket/prediction'
create_batch_prediction_job_dedicated_resources_sample(key_path,project,location,model_display_name,job_display_name,
gcs_source,gcs_destination)
OUTPUT:
92 current state:
JobState.JOB_STATE_RUNNING
INFO:google.cloud.aiplatform.jobs:BatchPredictionJob projects/my_project/locations/asia-south1/batchPredictionJobs/37737350127597649
The above output is being printed on the terminal over and over after every few seconds.
The issue that I have is that the python program calling this function keeps on running until it is force stopped. I have tried both batch_prediction_job.wait() & batch_prediction_job.wait_for_resource_creation() with the same results.
How do I start a batch_prediction_job without waiting for it to complete and terminating the program just after the job has be created?
I gave you the wrong instruction on the comments, change the parameter sync=False and the function should return just after be executed.
Whether this function call should be synchronous (wait for pipeline run to finish before terminating) or asynchronous (return immediately)
sync=False
def create_batch_prediction_job_dedicated_resources_sample(
# ...
sync: bool = False,
):
UPDATE - Adding more details:
Check here my notebook code where I tested it and its working:
You have to change the sync=False AND remove/comment the following print lines:
#batch_prediction_job.wait()
#print(batch_prediction_job.display_name)
#print(batch_prediction_job.resource_name)
#print(batch_prediction_job.state)
Your code edited:
def create_batch_prediction_job_dedicated_resources_sample(
key_path,
project: str,
location: str,
model_display_name: str,
job_display_name: str,
gcs_source: Union[str, Sequence[str]],
gcs_destination: str,
machine_type: str = "n1-standard-2",
sync: bool = False,
):
credentials = service_account.Credentials.from_service_account_file(key_path)
# Initilaize an aiplatfrom object
aiplatform.init(project=project, location=location, credentials=credentials)
# Get a list of Models by Model name
models = aiplatform.Model.list(filter=f'display_name="{model_display_name}"')
model_resource_name = models[0].resource_name
# Get the model
my_model = aiplatform.Model(model_resource_name)
batch_prediction_job = my_model.batch_predict(
job_display_name=job_display_name,
gcs_source=gcs_source,
gcs_destination_prefix=gcs_destination,
machine_type=machine_type,
sync=sync,
)
return batch_prediction_job
datetime_today = datetime.datetime.now()
model_display_name = 'test_model'
key_path = 'vertex_key.json'
project = '<my_project_name>'
location = 'asia-south1'
job_display_name = 'batch_prediction_' + str(datetime_today)
model_name = '1234'
gcs_source = 'gs://<my_bucket_name>/Cleaned_Data/user_item_pairs.jsonl'
gcs_destination = 'gs://<my_bucket_name>/prediction'
create_batch_prediction_job_dedicated_resources_sample(key_path,
project,location,
model_display_name,
job_display_name,
gcs_source,
gcs_destination,
sync=False,)
Results sync=False:
Results sync=True:

Testing a function with a #cache.memoize gives PicklingError

I have some tests for functions that use cache, for example:
Function:
#retry(stop=stop_after_attempt(3))
#cache.cached(timeout=60, key_prefix='resouce_group_list')
def get_azure_resource_groups():
data = []
resource_client = get_azure_resource_client()
for item in resource_client.resource_groups.list():
data.append(item)
return data
Test:
#patch("dev_maintenance.machines.get_azure_resource_client")
def test_get_azure_list_rg(get_azure_resource_client):
cache.clear()
data = []
with app.app_context():
ret = get_azure_resource_groups()
get_azure_resource_client.assert_called_once()
expected = get_azure_resource_client.return_value.resource_groups.list.return_value
assert len(get_azure_resource_client.return_value.method_calls) == 1
for item in expected:
data.append(item)
assert ret == data
cache.clear()
The above test works fine, it passes, no errors and the test is using cache.
But i got other tests, and the decorator here does not matter, it will give the same error if i change the decorator to #cache.cache:
Function:
#retry(stop=stop_after_attempt(3))
#cache.memoize(60)
def get_azure_machine_info(rg_name, machine_name, expand="instanceView"):
try:
compute_client = get_azure_compute_client()
return compute_client.virtual_machines.get(rg_name, machine_name, expand=expand)
except CloudError:
return None
Test:
#patch("dev_maintenance.machines.get_azure_compute_client")
def test_get_azure_machine_info (get_azure_compute_client):
cache.delete_memoized(get_azure_machine_info)
with app.app_context():
ret = get_azure_machine_info("rg1", "m1")
print(ret)
get_azure_compute_client.assert_called_once()
assert len(get_azure_compute_client.return_value.method_calls) == 1
assert (
ret == get_azure_compute_client.return_value.virtual_machines.get.return_value
)
get_azure_compute_client.return_value.virtual_machines.get.assert_called_once_with(
"rg1", "m1", expand="instanceView"
)
cache.delete_memoized(get_azure_machine_info)
Now here the test fails with the error on this line ret = get_azure_machine_info("rg1", "m1"):
value = None, from_value = PicklingError("Can't pickle <class 'unittest.mock.MagicMock'>: it's not the same object as unittest.mock.MagicMock")
> ???
E tenacity.RetryError: RetryError[<Future at 0x105c7c3d0 state=finished raised PicklingError>]
<string>:3: RetryError
I tried to mock the cache passing a patch decorator like:
#patch("dev_maintenance.machines.cache") or #patch("dev_maintenance.cache")
I tried to set the CACHE_TYPE to null in the test case, instantiating the cache object and passing the config:
cache = Cache()
cache.init_app(app, config={"CACHE_TYPE": "redis"})
but no success so far, any help?
This is a reference to an old answer, but I think that generally MagicMock objects aren't meant to be pickled: https://github.com/thadeusb/flask-cache/issues/52
That error message is different though, and this is more similar to what you are seeing:
Is there a way to make python pickle ignore "it's not the same object " errors
Maybe you could replace the domain prefix to the class like the answer above, but I am not sure it will overcome the other difficulties of pickling a MagicMock class:
`#patch("__main__.get_azure_compute_client")`
I'am writing the answer here for people that need to test functions that are cached with flask-caching and have the same error then me.
What i needed was to create an Object inside the test and make the mock_value = Object like this:
First i create a simple class:
class MachineInfo(object):
pass
Then in my test:
#patch("dev_maintenance.machines.get_azure_compute_client")
def test_get_azure_machine_info (get_azure_compute_client):
cache.clear()
expected_res = MachineInfo()
expected_res.id = "id"
expected_res.name = "machine1"
expected_res.location = "location"
expected_res.hardware_profile = "hardware"
expected_res.storage_profile = "storage"
expected_res.network_profile = "network_profile"
get_azure_compute_client.return_value.virtual_machines.get.return_value = expected_res
res = get_azure_machine_info("rg1", "m1")
assert res == expected_res
cache.clear()
Then i could assert function_call() == Object or function_call() == mock.return_value
This simulates what the actual azure returns, an object, so i just make the mock return the object that i created so i can simulate the function itself.

Browser shows variable refered before assign

here is the image of error that i am getting in the browser
I am new to python and hardly tried to figure out the problem of usese of variable from another if statement in the same function
here is my code:
def post(self, request, **kwargs):
selected_membership_type = request.POST.get('membership_type')
user_membership = get_user_membership(request)
user_subscription = get_user_subscription(request)
selected_membership_qs = Membership.objects.filter(
membership_type=selected_membership_type)
if selected_membership_qs.exists():
selected_membership = selected_membership_qs.first()
'''
==========
VALIDATION
==========
'''
# selected_membership = selected_membership_qs.first()
if user_membership.membership == selected_membership:
if user_subscription == None:
messages.info(request,"You already have this membership.Your \
next payment is due {}".format('get this value from stripe'))
return HttpResponseRedirect(request.META.get('HTTP_REFERER'))
The problem is the following:
if selected_membership_qs.exists():
selected_membership = selected_membership_qs.first()
You are only assigning selected_membership if the if is True.
So in your case you are getting the Variable referenced before assignment error because the if is False.
Therefore selected_membership is never assigned.
If you do something like this
selected_membership = None
if selected_membership_qs.exists():
selected_membership = selected_membership_qs.first()
it should work.

How to link a static library using WAF?

I'm using OpenSSL in my C++ program, and I need to link crypto and ssl with it. If it were for example gcc, I would just pass:
-lcrypto -lssl
I am adding this dependency in Network-Simulator 3.
But I don't know how to do this in WAF. How should I add them as a dependency?
First you need to check in configure if the library is available, then you can build it.
def configure(cnf):
# other parameters omitted for brevity
cnf.check(lib=["crypto", "ssl"])
def build(bld):
# other parameters omitted for brevity
bld(use=["crypto", "ssl"])
You could also use the uselib_store parameter if you don't want to repeat the libraries:
cnf.check(lib=["crypto", "ssl"], uselib_store=["libs"])
bld(use=["libs"])
I tried adding CRYPTOPP instead of Crypto because I could do the same work with CRYPTOPP.
This is how it worked for me.
The below code needs to be added/edited in wscript found in ns-3.xx directory.
def configure(conf):
#other parameters removed
env = conf.env
conf.env['cryptopp'] = conf.check(mandatory=True, lib='cryptopp', uselib_store='CRYPTOPP')
conf.env['sph'] = conf.check(mandatory=True, lib='sph', uselib_store='SPH')
conf.env.append_value('CXXDEFINES', 'ENABLE_CRYPTOPP')
conf.env.append_value('CCDEFINES', 'ENABLE_CRYPTOPP')
conf.env['lssl'] = conf.check(mandatory=True, lib='ssl', uselib_store='OPENSSL')
conf.env.append_value('CXXDEFINES', 'ENABLE_SSL')
conf.env.append_value('CCDEFINES', 'ENABLE_SSL')
.....
def create_ns3_program(bld, name, dependencies=('core',)):
program = bld(features='cxx cxxprogram')
#other parameters removed
program.use = program.ns3_module_dependencies
if program.env['ENABLE_STATIC_NS3']:
if sys.platform == 'darwin':
program.env.STLIB_MARKER = '-Wl,-all_load,-lcryptopp,-lsph'
else:
program.env.STLIB_MARKER = '-Wl,-Bstatic,--whole-archive,-lcryptopp,-lsph'
program.env.SHLIB_MARKER = '-Wl,-Bdynamic,--no-whole-archive,-lcryptopp,-lsph'
else:
if program.env.DEST_BINFMT == 'elf':
# All ELF platforms are impacted but only the gcc compiler has a flag to fix it.
if 'gcc' in (program.env.CXX_NAME, program.env.CC_NAME):
program.env.append_value ('SHLIB_MARKER', '-Wl,--no-as-needed,-lcryptopp,-lsph')
return program
.......
def add_scratch_programs(bld):
all_modules = [mod[len("ns3-"):] for mod in bld.env['NS3_ENABLED_MODULES']]
try:
for filename in os.listdir("scratch"):
if filename.startswith('.') or filename == 'CVS':
continue
if os.path.isdir(os.path.join("scratch", filename)):
obj = bld.create_ns3_program(filename, all_modules)
obj.path = obj.path.find_dir('scratch').find_dir(filename)
obj.source = obj.path.ant_glob('*.cc')
obj.target = filename
obj.name = obj.target
obj.install_path = None
#Add the below paramters
obj.uselib = 'CRYPTOPP'
obj.uselib = 'SPH'
obj.uselib = 'OPENSSL'
elif filename.endswith(".cc"):
name = filename[:-len(".cc")]
obj = bld.create_ns3_program(name, all_modules)
obj.path = obj.path.find_dir('scratch')
obj.source = filename
obj.target = name
obj.name = obj.target
obj.install_path = None
#Add the below paramters
obj.uselib = 'CRYPTOPP'
obj.uselib = 'SPH'
obj.uselib = 'OPENSSL'
except OSError:
return
This basically adds all paramters at runtime to the simulator you are running from scratch.
PS: This script is specific to ns-3.

Bdbquit error with Django debugging

I am a newbie for Django. Although I don't use the pdb for debugging but I used before and I removed all pdb methods, I get the Bdbquit error with following explanation:
/home/mastersnack/dpt2/app/views.py in start
version = request.session['version']
My related code is:
def start(request):
global Big_Matrix
if 'version' in request.session:
version = request.session['version']
else:
try:
version = Version.objects.order_by('-id')[0]
except IndexError:
return redirect('/')
if not request.user.id:
user = User(guest=True, date_joined=datetime.datetime.now(),
last_login=datetime.datetime.now())
user.save()
request.session["USER_ID"] = user.id
return redirect('/start')
rnd = Round(version=version, user=request.user, date=datetime.datetime.now())
rnd.save()
products = Product.objects.filter(version=version, selectable=True)
(a, b, c) = random.sample(products, 3)
poll = Poll(round=rnd, product_a= a, product_b= b, product_c= c,
date=datetime.datetime.now())
# (a,b,c)=get_meals(Big_Matrix, [])
# poll = Poll(round=rnd, product_a=Product.objects.get(stimuliNum = a, version =
version), product_b=Product.objects.get(stimuliNum = b, version = version),
product_c=Product.objects.get(stimuliNum = c, version = version),
date=datetime.datetime.now())
poll.save()
# (a,b,c)=get_meals(BigMatrix.objects.get(version = version).matrix, [])#bad_ functions)
# poll = Poll(round=rnd, product_a=Product.objects.get(oid = a, version = version), product_b=Product.objects.get(oid = b, version = version), product_c=Product.objects.get(oid = c, version = version), date=datetime.datetime.now())
# poll.save()
return redirect('/preferences')
Should I check my code again for the method maybe I forget to remove?
Make sure that you have removed all pdb methods and that you save your code.
After that if you are running the Django server, restart it.
That should do the trick