how can I do multi update with $ symbol in mongo engine - mongoengine

how can I do multi update with $ symbol with mongo engine in .py file, give any small example.

Refer to Atomic Updates in the docs:
Foo.objects.all().update(set__bar='baz')

>>> data = dict(set__real_rate=1, set__rate=1, set__change=1, set__variance=1, set__tags=[], set__cloud={}, set__description='not much')
>>> Grid.objects(id='tv').update(upsert=True, **data)
1

Theres examples in the test suite for mongoengine:
https://github.com/MongoEngine/mongoengine/blob/master/tests/queryset.py#L313-382
A quick example:
class BlogPost(Document):
title = StringField()
tags = ListField()
BlogPost.drop_collection()
BlogPost(title="ABC", tags=['mongoEngien']).save()
BlogPost.objects(tags="mongoEngien").update(set__tags__S="MongoEngine")

Related

where can I find make_labels?

I do not find make_labels
I thought it would be part of the independent package utils. But I guess it was part of featuretools.
it featuretools.utils
you just have make_temporal_cutoffs instead. So how do you use that? Waht would be the translation of the example code:
label_times = pd.concat([utils.make_labels(es=instacart_es,
product_name = "Banana",
cutoff_time = pd.Timestamp('March 15, 2015'),
prediction_window = ft.Timedelta("4 weeks"),
training_window = ft.Timedelta("60 days"))
found out!
make_label is not part of featuretool but of the example repository
https://github.com/Featuretools/predict-next-purchase

Creating and mounting volumes with docker.py

I've been looking through the documentation and some tutorials but I cannot seem to find anything current on how to create a volume using the docker.py library. Nothing I've found appears to be current as the create_host_config() method appears to be non-existent. Any help in solving this issue or a push in the right direction would be greatly appreciated. Thanks to all of you.
I've searched the documentation on:
https://docker-py.readthedocs.io/en/stable/
https://github.com/docker/docker-py
I tried using this old stack overflow example:
How to bind volumes in docker-py?
I've also tried the client.volumes.create() method.
I'm trying to write a class to make docker a bit easier for most people to deal with in python.
import docker
VOLUMES = ['/home/$USER', '/home/$USER/Desktop']
def mount(volumes):
mount_points = []
docker_client = docker.from_env()
volume_bindings = _create_volume_bindings(volumes)
host_config = docker_client.create_host_config(binds=volume_bindings)
def _create_volume_bindings(volumes):
volume_bindings = {}
for path in range(len(volumes)):
volume_bindings[volumes[path]] = {'bind': 'mnt' + str(path + 1),
'mode': 'rw'}
return volume_bindings
Perhaps you want to use the Low-level API client?
If yes, you can try to replace the line
docker_client = docker.from_env()
with
docker_client = docker.APIClient(base_url='unix://var/run/docker.sock')
That one has the create_host_config() method.

Obtain the desired value from the output of a method Python

i use a method in telethon python3 library:
"client(GetMessagesRequest(peers,[pinnedMsgId]))"
this return :
ChannelMessages(pts=41065, count=0, messages=[Message(out=False, mentioned=False,
media_unread=False, silent=False, post=False, id=20465, from_id=111104071,
to_id=PeerChannel(channel_id=1111111111), fwd_from=None, via_bot_id=None,
reply_to_msg_id=None, date=datetime.utcfromtimestamp(1517325331),
message=' test message test', media=None, reply_markup=None,
entities=[], views=None, edit_date=None, post_author=None, grouped_id=None)],
chats=[Channel(creator=..............
i only need text of message ------> test message test
how can get that alone?
the telethon team say:
"This is not related to the library. You just need more Python knowledge so ask somewhere else"
thanks
Assuming you have saved the return value in some variable, say, result = client(...), you can access members of any instance through the dot operator:
result = client(...)
message = result.messages[0]
The [0] is a way to access the first element of a list (see the documentation for __getitem__). Since you want the text...:
text = message.message
Should do the trick.

How to get all users in organization in GitHub?

I try to get all users in my organization in GitHub. I can get users, but I have problem with pagination - I don't know how many page I have to get around.
curl -i -s -u "user:pass" 'https://api.github.com/orgs/:org/members?page=1&per_page=100'
Of course I can iterate all the pages until my request will not return "0", but i think this is not very good idea )
Maybe GitHub have standard method for get all users in organization?
According to Traversing with Pagination, there should be a Link response header, such as:
Link: <https://api.github.com/orgs/:org/members?page=2>; rel="next", <https://api.github.com/orgs/:org/members?page=3>; rel="last"
These headers should give you all the information needed so that you can continue getting pages.
For performance reasons, I do not think that any API exists to bypass pagination.
Here is my github-users script for instance:
#!/usr/bin/env ruby
require 'octokit'
Octokit.configure do |c|
c.login = '....'
c.password = '...'
end
get = Octokit.org(ARGV.first).rels[:public_members].get
members = get.data
urls = members.map(&:url)
while members.size > 0
next_url = get.rels[:next]
next members = [] unless next_url
get = next_url.get
members = get.data
urls << members.map(&:url)
end
puts urls
e.g. github-members stackexchange gives:
https://api.github.com/users/JasonPunyon
https://api.github.com/users/JonHMChan
https://api.github.com/users/NickCraver
https://api.github.com/users/NickLarsen
https://api.github.com/users/PeterGrace
https://api.github.com/users/bretcope
https://api.github.com/users/captncraig
https://api.github.com/users/df07
https://api.github.com/users/dixon
https://api.github.com/users/gdalgas
https://api.github.com/users/haneytron
https://api.github.com/users/jc4p
https://api.github.com/users/kevin-montrose
https://api.github.com/users/kirtithorat
https://api.github.com/users/kylebrandt
https://api.github.com/users/momow
https://api.github.com/users/ocoster-se
https://api.github.com/users/robertaarcoverde
https://api.github.com/users/rossipedia
https://api.github.com/users/shanemadden
https://api.github.com/users/sklivvz
If you are using Python and requests library then get the header response and split it like below to get the last page number
last_page_num = int(r.headers["link"].split(",")[-1].split('&page=')[-1][0])
You can use the PyGithub Python library implementing the GitHub API v3 https://pygithub.readthedocs.io/en/latest/
g = Github("ghp_your-github-token")
for member in g.get_organization("my-org").get_members():
print(member.login, member.name, member.email)

Is there a way to use the low level mongoDB backend from persistent-mongoDB?

In the SQL version of persistent it appears that direct access to SQL is done with rawSql. Is there a similar way to access low level commands from the mongoDB backend?
It turns out to be much easier than I thought. Just import Database.MongoDB and use the raw driver commands inside runDB. Example:
import Database.MongoDB
...
postCommentR :: DocumentId -> Handler Value
postCommentR documentId = do
comment <- commentOr400
let idString = toPathPiece documentId
contents = commentBody comment
runDB $ DB.modify (DB.select ["_id" DB.=: idString] "Document") ["$push" DB.=: ["comments" DB.=: contents]]
returnJson $ object []

Resources