Since from is a special python keyword I am not able to pass it pyes.es.search function. It give syntax error. pyes.es.search(maf, "twitter", "tweet", sort="timestamp", size=2, from=3) . I passed keyword arguments containing from also as below but from did not work while other worked.
keywords = {'sort': 'timestamp', 'size':3, 'from':2}
r = pyes.es.search(maf, "twitter", "reply",**keywords)
This problem also available for another python elasticsearch module here here. In search function interface there is from argument.
Did you try with start parameter?
It sounds like the one to use.
See http://pyes.readthedocs.org/en/latest/references/pyes.queryset.html#pyes.queryset.QuerySet.start
Related
#python
sql="UPDATE bebliothequee SET title=:titl,author=:autho,ISBN=:ISB WHERE oid='{}'.format(oid.get())",title='{}'.format(title_editor.get()),author='{}'.format(author_editor.get()),ISBN='{}'.format(ISBN_editor.get()),oid='{}'.format(record_id.get())
Your first mistake is that you didn't read Stackoverflow documentation how to create good question. So you didn't add all details in question - and we can't read in your mind - and we can't help it.
Next mistake: you put code in comment but you should put it in question so it would be more readalbe but what is more important: all people could see it and help you.
Code shows that you try to create sql =... with all arguments for execute() and later use it in execute() but it doesn't work this way. You can't assing positional and named values to variable and later put it in function. You should use it directly in execute() or you should create list/tuple with positional variables and dictionary with named variables.
BTW: you don't need '{}'.format() to convert it to strings. You could use str() but I think execute() should convert it automatically.
query = "UPDATE bebliothequee SET title=:title, author=:author, ISBN=:ISBN WHERE oid=:oid"
execute(query, title=title_editor.get(), author=author_editor.get(), ISBN=ISBN_editor.get(), oid=record_id.get())
Other problem is that you use :titl but you should use full name :title like in argument title=....
You have also oid='{}'.format(oid.get() inside query which is totally useless.
BTW: Eventually you can create dictionary
args = {
'title': title_editor.get(),
'author', author_editor.get(),
'ISBN': ISBN_editor.get(),
'oid': record_id.get(),
}
and then you can use ** to unpack it as named arguments
execute(query, **args)
I am trying to create a parameter that is required using the Python Dialogflow API v2. My current code is as follows:
parts.append(Intent.TrainingPhrase.Part(text=value,
alias=value,
entity_type='#' + value))
This works to create the parameter, but it is not required. The obvious first attempt was to add a required=True param, but that was not expected by the function call.
Looking at their docs (https://googleapis.dev/python/dialogflow/latest/gapic/v2/types.html) there does not seem to be any required/mandatory field.
I started to dig a little into their code, and found similar - there does not seem to be a required or mandatory field, except when creating the parameter?
https://github.com/googleapis/dialogflow-python-client-v2/blob/7bf592684b4d5df0cd1f66dd414efe2350d0461e/dialogflow_v2/proto/intent.proto
Ah, got it. My mistake was that you can create parameters for an intent as I was doing above, and it will add the parameter, but not required. You can explicitly specify a parameter as well, which is what has the mandatory field. Like this:
if len(parameters) > 0:
for parameter in parameters:
intent_parameter = Intent.Parameter(
display_name=parameter.name,
value=parameter.name,
entity_type_display_name='#' + parameter.name,
mandatory=True
)
intent.parameters.extend([intent_parameter])
I am trying to follow syntax of the pyparticleio.ParticleCloud package. Using the following command, my code works correctly "particle_cloud.boron1.led('on')" (hardcoded values)
I want to pass portions of the command, "boron1" and "on" as variable. I'm trying to figure out how to use those variables to act in the same way as if i'd hardcoded the values.
My python level is very beginner.
command_list['boron1','on']
device = command_list[0]
function_1 = command_list[1]
access_token = "ak3bidl3xxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
particle_cloud = ParticleCloud(username_or_access_token=access_token)
#particle_cloud.boron1.led('on') #hardcoded example that works
particle_cloud.device.led(function_1) #what i would like to work
If you set device to the actual object, you can call methods on the object. Example:
device = particle_cloud.boron1 # Or whatever you like
arg = 'on' # Also make this whatever you like
device.led(arg) # Same as: particle_cloud.boron1.led('on')
Python has a built in function called exec
It allows you to take a string, and have Python execute it as code.
A basic example based on the code you provided would look like this:
command_list['boron1','on']
device = command_list[0]
function_1 = command_list[1]
exec('particle_cloud.' + device + '.led("' + function_1 + '")')
This is a bit ugly, but there are different ways to compose strings in Python such as using join or format so depending on your real code you may be able to build something nice.
Just be careful not to pass raw user input to exec!
I can cause all kinds of trouble from errors to security issues.
I believe you could use getattr() (in Python3: https://docs.python.org/3/library/functions.html#getattr ) :
pcdevice = getattr(particle_cloud, device)
pcdevice.led(function_1)
(BTW, I woudln't name the string 'on' with the label 'function_1' as the variable name implies that this option is a function when it is a string. Also, the above may now work depending on the properties of your library object ParticleCloud.)
I'm looking at the reddit source code and ran into a route that looks like the following:
mc('/prefs/:location', controller='forms', action='prefs', location='options')
Searching the documentation here, I can't find anything related to the location attribute:
https://thejimmyg.github.io/pylonsbook/en/1.0/urls-routing-and-dispatch.html
What is location='options' supposed to do for this route?
routes.Mapper.connect() takes *args and **kwargs as arguments. Since locations='options' is a valid **kwargs entry and in a valid location, it will be accepted as a valid passed variable. I don't have the source code available, so I don't know what connect() will do with this variable. I'm guessing it simply attaches the extra name=value pairs to the created URLs.
You can find more documentation here: http://routes.readthedocs.io/en/latest/modules/mapper.html#routes.mapper.Mapper.connect
I was looking at this code on the Google Map API website:
import simplejson, urllib
GEOCODE_BASE_URL = 'http://maps.googleapis.com/maps/api/geocode/json'
def geocode(address,sensor, **geo_args):
geo_args.update({
'address': address,
'sensor': sensor
})
url = GEOCODE_BASE_URL + '?' + urllib.urlencode(geo_args)
result = simplejson.load(urllib.urlopen(url))
print simplejson.dumps([s['formatted_address'] for s in result['results']], indent=2)
if __name__ == '__main__':
geocode(address="San+Francisco",sensor="false")
I noticed that in the geocode function, when we actually apply the function we don't use the geo_args dictionary when we call the function, but we instead use it to initialize a dictionary we update in the next lines. What is the benefit of using this, as opposed to initializing the dictionary within the function itself? It makes the code a bit less clear so I assume there is a reason for doing it.
geo_args is used. The literal dictionary
{
'address': address,
'sensor': sensor
}
updates geo_args, not the other way around.
The reason it's like this looks to me that address and sensor are required arguments. This structure allows the function to enforce that requireness, and also allow them to be passed positionally. The .update() is just there to consolidate all the arguments into one so they can be given to urllib.urlencode.
Not clear? This is the normal behaviour of **kwargs (the keyword argument dictionary).
So when you see something with **, a bunch of named parameters or keyword arguments will be passed.
Seeing the other answers. I probably misunderstood your question. I agree with the other answers: address and sensor are required.