Python MySQL if variable does not exists still submit - python

I'm doing some queries ( inserts ) to a database based on some input.
However not all the times I get all the data from the user. I still would like though to insert the data that I received. I have a table with close to
10 columns but in the data I also have some arrays.
When I'm trying to insert something I get an exception that the say input['name'] does not exists and the query is not executed.
Is there some way to quickly manage that? If a variable does isn't defined simply throw a warning like in PHP and not break the whole loop.
New to python and only thing I can think of is to check for every single variable but I'd really hope there's something more simpler than this and quicker.

Do input.get('name')
From the docs https://docs.python.org/2/library/stdtypes.html#dict.get
Return the value for key if key is in the dictionary, else default.
If default is not given, it defaults to None, so that this method never raises a KeyError.

You should look into exception handling. It sounds like you need to use an try-except-else where you're making use of input['name']

Related

Parsing JSON with Python - Case Sensitivity for Keys

I'm trying to read a json from mulitple REST end points using json.load - the key I'm interested in however has a different case depending on which end point (some are 'name' others are 'Name'). I've tried the following to get around it:
for x in urls:
response_json = json.load(urllib.request.urlopen(x))
try:
projects.append(response_json['attributes']['Name'])
print(response_json['attributes']['Name'])
except:
projects.append(response_json['attributes']['name'])
print(response_json['attributes']['name'])
However I'm getting KeyErrors for both permutations of the 'name' key. The code works fine if I remove the TRY...EXCEPT and only specify one case but then I need to know what is it for each URL. Why would that be? is there a better approach?
You should only use try and catch for error handling.
Instead of using try and catch to split your code, you can use a simple if-else condition check. It'll allow you to check for more conditions.
Keep in mind that the below might not be the exact code since I don't have your JSON, however, this should guide you towards the solution here.
if 'Name' in response_json['attributes']:
print(response_json['attributes']['Name'])
elif 'name' in response_json['attributes']:
print(response_json['attributes']['name'])
else:
print(response_json['attributes'].keys())

SQLAlchemy core - get a list of bindparams in a query, or check if one exists by name?

With sqlalchemy core you can bind a parameter and then fill it in at execution time, e.g.
query = select([my_table]).limit(bindparam('lim'))
session.execute(query, lim=10)
I have it so that the queries received may or may not be using certain parameters. So I'd like a way to be able to verify if a parameter exists or even to get a list of the parameters that have to be substituted at execution time. E.g. this might be done to allow limits in the query like above.
The values which may need to be substituted will be known, so it's just a case of checking if they're actually there. At the moment as a small temporary workaround I have found I can set the names to complex things like random strings of digits, and then just check if those exist in the query's string form. Of course other solutions exists like storing/passing which ones have/haven't been used, but I'm looking to see if there's a way just to check which ones exist. and are being expected.
You can get the parameters from the compiled query, like this:
>>> q = select([test1]).limit(bindparam('lim'))
>>> c = q.compile()
>>> c.params
{'lim': None}

Extract single True/False value from JSON Twitter response ( Tweepy and Python )

I am trying to write a little bit of code in an app that checks if a user is following you or not. I just want the code to return a True/False value that I can put in an if loop.
The code is:
user_id = '1234567890'
print api.show_friendship(target_id=user_id)
And it returns all this JSON, which I know nothing about but want the 'false' value from the second array for the other user under 'following' (or, alternatively, the true/false from the first array under 'followed_by'... either way is fine!):
{"relationship":{"source":{"id":0000000000,"id_str":"0000000000","screen_name":"auth_user","following":false,"followed_by":false,"following_received":false,"following_requested":false,"notifications_enabled":false,"can_dm":false,"blocking":false,"blocked_by":false,"muting":false,"want_retweets":false,"all_replies":false,"marked_spam":false},"target":{"id":123456789,"id_str":"123456789","screen_name":"other_user","following":false,"followed_by":false,"following_received":false,"following_requested":false}}}
How can I make my code return the true/false for the "following":false part?
(or, the "followed_by":false part in the first array - each array is from the user's "viewpoint" of the relationship)
Nice, solved it immediately after posting.
Try:
print json.loads(api.show_friendship(target_id=user_id))["relationship"]["target"]["followed_by"]
You can work through the JSON array by specifying each branch as you go through it (I'm sure many people know that lol, but I've never used JSON!)...
print api.show_friendship(target_screen_name=screen_name)[1].following
True

NDB Put Transactions are being Overwritten

I have a dictionary that I would like to write in whole to an NDB on App Engine. The problem is that only the last item in the dictionary is being written. I thought perhaps the writes were too fast so I put a sleep timer in with a very long wait of 20 seconds to see what would happen. I continually refreshed the Datastore Viewer and saw the transaction write, and then later get overwritten by the next transaction, etc. The table started out empty and the dictionary keys are unique. A simple example:
class Stats(ndb.Model):
desc= ndb.StringProperty(required = True)
count= ndb.IntegerProperty(required = True)
update = ndb.DateTimeProperty(auto_now_add = True)
class refresh(webapp2.RequestHandler):
def get(self):
statsStore = Stats()
dict = {"test1":0,"test2":1,"test3":2}
for key in dict:
statsStore.desc = key
statsStore.count = dict.get(key)
statsStore.put()
What will happen above is that only the final dictionary item will remain in the datastore. Again with a sleep timer I can see each being written but then overwritten. I am using this on my local machine with the local development GAE environment.
Appreciate the help.
The problem with your original code is that you're reusing the same entity (model instance).
During the first put(), a datastore key is generated and assigned to that entity. Then, all the following put() calls are using the same key.
Changing it to create a new model instance on each iteration (the solution you mention in your comment) will ensure a new datastore key is generated each time.
Another option would be to clear the key with "statsStore.key = None" before calling put(). But what you did is probably better.
Not sure what you are trying to do, but here are some hopefully helpful pointers. If you want to save the dict and then re-use it later by reading from the database, then change your string to a text property, import json, and save the dict as a json string value using json.dumps(). If you want to write an entity for every element in your dict, then you will want to move your statsStore class creation line inside the for loop, and finish the loop process by adding each Stats() classes to an array. Once the loop is done, you can batch put all the entities in the array. This batch approach is much faster than including a put() inside your loop which is most often a very non-performant design choice. If you just want to record all the values in the dict for later reference, and you have a value that you can safely use as a delimiter, then I would create two empty arrays prior to your loop, and append each desc and count inside the respective array. Once outside the array, you can save these values to two text properties in your entity by joining the arrays using the delimiter string. If you do this, then strongly suggest using urllib.quote() to escape your desc text value when appending it so at to avoid conflicts with your delimiter value.
Some final notese: You should be careful using this type of process with a StringProperty. You might easily exceed the string limit size depending on the number of items, and/or the length of your desc values. Also remember your items in the dict may not come out in the order you intend. Consider something like: "for k, v in sorted(mydict.items()):" HTH, stevep

Search a database for elements including a string variable

I'm pretty new to SQLite and Python and have run into a bit of confusion. I'm trying to return all elements in a column that contain a substring which is passed to a function as a variable in Python. My code is running, but it's returning an empty result instead of the correct result.
Here's the code with the names generalized:
def myFunc(cursor,myString):
return cursor.execute("""select myID from Column where name like '%'+?'%' """,(myString,))
Like I said, the code does run without error but returns an empty result instead of the result that I know it should be. I'm assuming it has something to do with my use of the wildcard and/or question mark, but I can't be sure. Anyone have any ideas? Thanks in advance for your time/help! Also, this is my first post, so I apologize in advance if I missed any of the recommended protocols for asking questions.
Well, '%'+?'%' definitely isn't going to work—you're trying to concatenate with + on the left, but with no operator…
You can compute LIKE-search fields if you do it right—'%'+?+'%', in this case. That will cause problems with some databases (from not working, to doing a less efficient search), but, at least according to CL.'s comment, sqlite3 will be fine.
But the easy thing to do is to just substitute a complete parameter, rather than part of one. You can put % into the parameters, and it'll be interpreted just fine. So:
return cursor.execute("""select myID from Column where name like ?""",
('%'+myString+'%',))
And this also has the advantage that if you want to do a search for initial substrings ('foo%'), it'll be the same SQL statement but with a different parameter.
Try this:
def myFunc(cursor,myString):
return cursor.execute('select myID from Column where name like "{0}"'.format(myString))

Categories

Resources