sorry for my ignorance but my expectation is that this would work:
from google.appengine.ext import ndb
from models import myModels
delete_futures = []
delete_futures.append(ndb.delete_multi_async(myModels.Kind1.query().fetch(999999, keys_only=True)))
delete_futures.append(ndb.delete_multi_async(myModels.Kind2.query().fetch(999999, keys_only=True)))
ndb.Future.wait_all(delete_futures)
but it throws "TypeError: list objects are unhashable".
perhaps use .extend to create a single list rather then a list of lists?
Wait until all Futures in the passed list are done.
Not expecting your passed list of lists maybe.
delete_futures = []
delete_futures.extend(ndb.delete_multi_async(myModels.Kind1.query().fetch(999999, keys_only=True)))
delete_futures.extend(ndb.delete_multi_async(myModels.Kind2.query().fetch(999999, keys_only=True)))
https://developers.google.com/appengine/docs/python/ndb/futureclass#Future_wait_all
each call to delete_multi_async returns a list of futures, so your delete_futures list is a list of lists. Change your appends to extend and it should work
Related
Sorry for any broken english...
So, basically, I have a list containing messages and their destinies:
messages = [(id_1, msg_1), (id_2, msg_2), (id_3, msg_3),]
What I want is to use a: async for msg in messages however, my list, obviously, isn't asynchronously iterable (I am using sqlite3) how can I convert it to a asynchronously iterable list?
You do not need "asynchronously iterable lists". You can just iterate normally:
for (id, msg) in messages:
# do things...
If you really need an "async iterable list", you can wrap it like so, however note this is probably very unnecessary (and it also requires a recent Python version):
async def make_async_iter(lst):
for item in lst:
yield item
...
async for item in make_async_list(messages):
# use item...
I have multiple numpy arrays with different objects. How can I give 'type-hint' to the Pycharm so that I can get methods of that class while coding? For example,
import numpy as np
class Test:
def do_task():
pass
data = [Test(), Test(), Test()]
my_array = np.array(data, dtype=Test)
def test_hint(array:np.ndarray): # <-- I can give typehint as array but I also want to give hint about what kind of array it is.
for a in array:
a.... # Here I want Pycharm to give me list of methods from 'Test' class
I can always explicitly mention what kind of object it is like following,
for a in array:
temp = a # type: Test
temp... # Here Pycharm will give me all the suggestions.
Is there any way I can provide type-hint in the constructor so that I do not need to write additional line of code to declare type of data? I tried looking at python's type module for some clue, but couldn't find any.
I extracted from a previous response an Object of tuple with the following regex :
.check(regex(""""idSc":(.{1,8}),"pasTemps":."codePasTemps":(.),"""").ofType[(String,String)].findAll.saveAs ("OBJECTS1"))
So I get my object :
OBJECTS1 -> List((1657751,2), (1658105,2), (4557378,2), (1657750,1), (916,1), (917,2), (1658068,1), (1658069,2), (4557379,2), (1658082,1), (4557367,1), (4557368,1), (1660865,2), (1660866,2), (1658122,1), (921,1), (922,2), (923,2), (1660875,1), (1660876,2), (1660877,2), (1658300,1), (1658301,1), (1658302,1), (1658309,1), (1658310,1), (2996562,1), (4638455,1))
After that I did a Foreach and need to extract every couple to add them in next requests So we tried :
.foreach("${OBJECTS1}", "couple") {
exec(http("request_foreach47"
.get("/ctr/web/api/seriegraph/bydates/${couple(0)}/${couple(1)}/1552863600000/1554191743799")
.headers(headers_27))
}
But I get the message : named 'couple' does not support index access
I also though that to use 2 regex on the couple to extract both part could work but I haven't found any way to use a regex on a session variable. (Even if its not needed for this case but possible im really interessed to learn how as it could be usefull)
If would be really thankfull if you could provided me help. (Im using Gatling 2 but can,'t use a more recent version as its for work and others scripts have been develloped with Gatling2)
each "couple" is a scala tuple which can't be indexed into like a collection. Fortunately the gatling EL has a function that handles tuples.
so instead of
.get("/ctr/web/api/seriegraph/bydates/${couple(0)}/${couple(1)}/1552863600000/1554191743799")
you can use
.get("/ctr/web/api/seriegraph/bydates/${couple._1}/${couple._2}/1552863600000/1554191743799")
I need to construct a logical query with a repeated property and can't get it to work.
I have a list object with topics.
topics = [u'string1', u'string2', ...]
I have a query object:
videos = Video.query()
videos.count()
=> 19
topics is a repeated string property
class Video
topics = ndb.StringProperty(repeated=True)
I want to return videos that have a topic string1 OR string2. I also don't know the length of the list object before or I could just construct the query the long way with logical operators.
I tried doing this like the documentation suggests
videos.filter( Video.topics.IN([topics]) )
but that throws the error that IN expected a string not a list object.
How do I do this?
Looks like topics is already a list. So you need to pass it without another list around it:
videos.filter( Video.topics.IN(topics) )
For an arry of topics, you can use:
Video.query(Video.topics.IN(topics))
Or for a single string:
Video.query(Video.topics == topic)
source: https://cloud.google.com/appengine/docs/standard/python/ndb/queries
I want to be able to take a dynamically created string, say "Pigeon" and determine at runtime whether Google App Engine has a Model class defined in this project named "Pigeon". If "Pigeon" is the name of a existant model class, I would like to then get a reference to the Pigeon class so defined.
Also, I don't want to use eval at all, since the dynamic string "Pigeon" in this case, comes from outside.
You could try, although probably very, very bad practice:
def get_class_instance(nm) :
try :
return eval(nm+'()')
except :
return None
Also, to make that safer, you could give eval a locals hash: eval(nm+'()', {'Pigeon':pigeon})
I'm not sure if that would work, and it definitely has an issue: if there is a function called the value of nm, it would return that:
def Pigeon() :
return "Pigeon"
print(get_class_instance('Pigeon')) # >> 'Pigeon'
EDIT: Another way of doing it is possibly (untested), if you know the module:
(Sorry, I keep forgetting it's not obj.hasattr, its hasattr(obj)!)
import models as m
def get_class_instance(nm) :
if hasattr(m, nm) :
return getattr(m, nm)()
else : return None
EDIT 2: Yes, it does work! Woo!
Actually, looking through the source code and interweb, I found a undocumented method that seems to fit the bill.
from google.appengine.ext import db
key = "ModelObject" #This is a dynamically generated string
klass = db.class_for_kind(key)
This method will throw a descriptive exception if the class does not exist, so you should probably catch it if the key string comes from the outside.
There's two fairly easy ways to do this without relying on internal details:
Use the google.appengine.api.datastore API, like so:
from google.appengine.api import datastore
q = datastore.Query('EntityType')
if q.get(1):
print "EntityType exists!"
The other option is to use the db.Expando class:
def GetEntityClass(entity_type):
class Entity(db.Expando):
#classmethod
def kind(cls):
return entity_type
return Entity
cls = GetEntityClass('EntityType')
if cls.all().get():
print "EntityType exists!"
The latter has the advantage that you can use GetEntityClass to generate an Expando class for any entity type, and interact with it the same way you would a normal class.