using pull_all and push_all with python/mongoengine - python

I have this object in Mongo:
mystuff = ListField(ReferenceField(Asset, dbref=True))
I have a Python method that is supposed to update the Mongo object, prepending its mystuff Listfield value with a given value. Because Mongoengine doesn't yet have a way to insert an object into a certain point in a list (and has made it a low priority to add this function), I've tried to:
save the contents of the current list to a temporary variable (oldlist)
update the DB entry, emptying the mystuff list using the "pull_all" modifier (which is part of mongoengine)
update the DB entry again, pushing the newly added item to the mystuff list using update(push)
update the DB entry once again, using the "push_all" modifier and the oldlist variable to push the old stuff back onto the mystuff list.
It seems that "pull_all" requires some kind of modifier, but I'll be danged if I can figure out what it wants.
Anybody got any ideas? Of course the ideal situation would be to add an "insert_at" modifier to update(), but that's out of my hands. Life on the edge, etc.

The pull_all takes a list of elements you want to pull out of the list. In you're case I believe this will be oldlist.
However I think you're best bet is probably to retrieve the whole document with get, modify the mystuff field in the client code, and send it back with a save.
As you noted the tools for updating the document in place are limited.

Related

Vim fastest way to replace words

Sometimes I find myself wanting to replace just 2-3 long words in a program and I find it a bit painful in how I do it, just wondering if there is any Vim wizards out there that could give me a faster way of doing this:
var_wanted = {}
some_other_var = {}
def function1():
....
....
some_other_var.append(...)
....
some_other_var.append(...)
some_other_var.append(...)
....
Now lets say I want to replace some_other_var with var_wanted, now usually the way I do this is I will go to var_wanted use yiw to copy the word to register, then move to the first instance of some_other_var do viw to select the word, then do p to paste it in and replace the word. However this process is not repeatable, I can't go to the next instance of some_other_var and type . because for some reason now some_other_var is in my register as opposed to var_wanted, I do this so often that I feel like I'm losing years of my life. And yes I am aware of using search and replace :%s/some_other_var/var_watned/gc, but I feel like this instance is for so few replacements that its not worth typing that whole thing out. Please heeelp
End product wanted:
var_wanted = {}
some_other_var = {}
def function1():
....
....
var_wanted.append(...)
....
var_wanted.append(...)
var_wanted.append(...)
....
TL;DR:
Cursor on var_wanted: "ayiw (yank into register a).
Cursor on some_old_var: ciwCtrlraEsc (change word, insert contents of register a).
Put cursor on next some_old_var: . will do the previous action again.
Finding a keystroke series that's shorter than %s/some_old_var/var_wanted/g is going to be difficult.
because for some reason now some_other_var is in my register
viwp is implicitly deleting the selected word. Deleted text in vim goes into the register. You can avoid this with viw"_dP instead, which explicitly deletes into the null register so it does not get copied, and then puts. Typing this 3 times seems worse than the %s/ version.
If it's typing var_wanted and some_other_var that bothers you, you can yank them into registers (let's use "find and "replace for mnemonic purposes) via "fyiw and "ryiw when your cursor is in the right spots. Then you could just %s:<C-r>f:<C-r>r:g to do all the replaces (<C-R> means Ctrlr). The problem here is moving the cursor around and then yanking doesn't seem much faster than typing the word.
There's also changing things. If you had some_old_var., you could position your cursor on the word and then ciwvar_wanted, which would remove some_old_var and enter insert mode where you would type var_wanted. The benefit to this is after leaving insert mode, you could use . when your cursor is on the next instance of some_old_var and it would repeat the whole action, replacing some_old_var with var_wanted.
Another useful note is that if var_wanted is super long (and exists in this file already), you can use C-n to autocomplete after you've typed a few characters of the word. This would still work with . after you finished the insert.
If you really want to avoid typing any amount of var_wanted, you can use the Ctrlr in insert mode as well, meaning you can yank var_wanted by putting your cursor on it and using "ayiw (to yank into register a) and then putting your cursor on some_old_var and doing ciw<C-R>a to replace it.
As long as your desired register is the yank register:
:%s/some_other_var/\=#0/g
The above solution is good if you are trying to change a large number of occurences of the "some_other_var"
You can also use the "gn" approach, see :h gn, in my case I have:
:nnoremap c* *<c-o>cgn
copy the "var_wanted" move to the first "some_other_var" and then press:
c* ...................... triggers our mapping
<Ctrl-r>0 ................ paste the yank register
<Esc> .................... leave insert mode
. ....................... now press dot as much as you want
To learn more about the gn trick watch this amazing video:
http://vimcasts.org/episodes/operating-on-search-matches-using-gn/

How to store and get strings with concatenated variables from database without loosing variable value in python

I store in database a string with concatenated variables but when I fetch it, it behaves like string, and the variable values are not reflected.
Stored in database field I have:
"""\
Please visit the following link to grant or revoke your consent:
"""+os.environ.get("PROTOCOL")+"""://"""+os.environ.get("DOMAIN")+"""/consent?id="""+consentHash+""""""
I need to be able to fetch it in python and store it in a variable but have the concatenated variable values reflected:
someVariable = database['field']
But like this the concatenated variable values are not processed and the whole thing behaves like one string.
When I print(someVariable) I am expecting
Please visit the following link to grant or revoke your consent:
https://somedomain/consent?id=123
But instead I get the original stored string as in database field:
"""\
Please visit the following link to grant or revoke your consent:
"""+os.environ.get("PROTOCOL")+"""://"""+os.environ.get("DOMAIN")+"""/consent?id="""+consentHash+""""""
You can call eval on your string to have it, uh, evaluate the string as an expression.
Using eval is considered dangerous, because it can be used to do pretty much anything you could write code for, without knowing just what that code will be ahead of time. This is more of an issue when using it on strings provided from an outside source.

How to get the text of drop down values in squish tool?

I have to verify the text of drop down list elements. How can I verify the same using python script in squish tool ?
Naive approach:
Record (then replay) selecting each of the entries. Use exception handling to log accessing individual entries and be able to proceed to test script execution.
More flexible approach:
Recording selecting one of the entries. This gives you script code to make the open the drop down and the object name of the drop down list. Then use object.children() to get all child elements of the drop down list object.
Pseudo example:
drop_down_list = waitForObject(...)
children = object.children(drop_down_list)
test.verify("Entry 1", children[0].text)
(You have to check the properties of the children to see which actual property contains the text or whatever else you want to verify.)

TypeError: documents must be a non-empty list

I'm doing a program using Twitter API and MongoDB in 2.7 Python language.
I get a timeline and put it in a dictionary, which I want to store in a MongoDB database. To do this I have next code:
def saveOnBD(self, dic):
client = MongoClient("xxxx", "port")
db = client.DB_Tweets_User_Date
collection = db.tweets
collection.insert_many(dic)
I'm debbuging and dic it's not empty but I get next error:
TypeError: documents must be a non-empty list
How can I fix it?
I trying many options, but i solved that question changing the post method.
Instead of:
collection.insert_many(dic)
I used this:
collection.insert_one(dic)
I supose that, as I try to post only a variable(dic) and "insert_many()" is for many variables that retun me the error. That change solved me the question
you can either put in an entry before running the bulk entry function or use insert()
A list of documents must be passed to insert_many method
E.g.:
collection.insert_many([dic])

Converting string into an object instance name - python

I'm probably going about this all wrong but...
I am trying to populate a QTreeView from SQL data - using QAbstractItemModel (and having a great deal of trouble understanding it tbh). One of the tutorials I am following (the simplest) populates the Tree by simply calling new instances of the 'Node' and generating the model from the list. The Node has a name and a parentnode (as below). This is OK where you are generating the data within the program. This I can just about follow :)
However, I want to bring the data in from the table and use a string to identify the correct parentnode - mainly because if I am iterating over the records I won't be able to name each one using a separate variable(?). It will be for x in recs: node = Node("name", parentnode).
When I do this, I get the obvious error message that the string isnt the correct object and has no methods. Is there a way of using a string derived from my table to identify the correct 'parent' object (either that, or could somebody point me in the direction of a very basic Qtreeview model tutorial designed for very enthusiastic, but not necessary gifted learners).
rootNode = Node("Hips")
childNode0 = TransformNode("RightPirateLeg", rootNode)
childNode1 = Node("RightPirateLeg_END", childNode0)
childNode2 = CameraNode("LeftFemur", rootNode)
childNode3 = Node("LeftTibia", childNode2)
childNode4 = Node("LeftFoot", childNode3)
childNode5 = LightNode("LeftFoot_END", childNode4)
I realise that I am probably running before I can walk here and apologise in advance for my ignorance.
Are the strings the names of global variables? If so, you can access the value refenced by the global variable with globals()['name'], (replacing 'name' with the string name of the variable of course.)
Or, better yet, instead of littering variable names all over your global namespace
you could use a dict:
node={}
node['rootNode']=Node('Hips')
node['childNode0']=TransformNode('RightPirateLeg',node['rootNode'])
...
This makes it very easy to map between string names and values.

Categories

Resources