Error when calling flickrapi.photosets.getPhotos method - python

I am trying to use flickrapi from #sybren on python 3.4.
Therefore i cloned the main branch of the repo and installed the package.
Some function calls do work, but some give me this error:
Traceback (most recent call last):
File "D:\personal works\flickrWorks\flickr_derpage.py", line 20, in <module>
flickr.photosets.getPhotos(set_id)
TypeError: __call__() takes 1 positional argument but 2 were given
The call to the function is this one:
import flickrapi
import xml.etree.ElementTree as ET
# config stuff
api_key = 'fuhsdkjfsdjkfsjk'
api_secret = 'fdjksnfkjsdnfkj'
user_tbp_dev = "fednkjfnsdjkfnjksdn5"
# le program
flickr = flickrapi.FlickrAPI(api_key, api_secret)
sets = flickr.photosets.getList(user_id=user_tbp_dev)
set0 = sets.find('photosets').findall('photoset')
set_id = set0[0].get('id')
sett_photos = flickr.photosets.getPhotos(set_id)
print(ET.dump(sett_photos))
Another method which gives the same error is:
flickr.reflection.getMethodInfo("flickr.photos.search")
Any ideas what might i do wrong, or if the library has some issues (as the python3 branch is still under development).
Thanks!

The flickrapi expects the parameters to the functions to be named arguments, not positional. This works for me:
flickr.photosets_getPhotos(photoset_id=set_id, extras="license, date_upload, date_taken")
To get a list of the argument names for the Flickr calls, see the documentation here: https://www.flickr.com/services/api/flickr.photosets.getPhotos.html

Related

Why is my Discord Bot in GitHub not working?

When I run main.py it's giving me this error. I was just trying to replicate a bot from GitHub and I didn't know it would be this difficult, here is the GitHub:
https://github.com/Normynator/RagnaDBot
C:\testebot\RagnaDBot>python main.py
INFO:Config logging is enabled and set to: 10
DEBUG:Using proactor: IocpProactor
Traceback (most recent call last):
File "C:\testebot\RagnaDBot\main.py", line 29, in <module>
_settings = load.load_settings(_config)
File "C:\testebot\RagnaDBot\lib\load.py", line 11, in load_settings
document = yaml.load(document)
TypeError: load() missing 1 required positional argument: 'Loader'```
load.py:
#!/usr/bin/env python3
import yaml
import logging
from lib import mvp # required for yaml
from lib.mvp import MVP
def load_settings(path):
with open(path) as f:
document = f.read()
document = yaml.load(document) - i think the problem is possibly here
logging.debug(document)
return document
main.py:
# Path to the config file
_config = "config.yml"
_client = discord.Client()
_settings = load.load_settings(_config) - problem possibly be here too
_mvp_list = load.parse_mvp_list(_settings['mvp_list'])
_channel = discord.Object(id=_settings['channel_id'])
_debug_core = False
_time_mult = 60 # .sleep works with seconds, to get minutes multiply by 60
You are using PyYAML's old load() function which for the longest time defaulted to possible unsafe behavior on unchecked YAML. That is why in 2020
this default was finally deprecated.
If you don't have any tags in your YAML you should use:
document = yaml.safe_load(document)
if you do have tags in your YAML you can use
`yaml.load(document, Loader=yaml.FullLoader)`
, but note that would require registering of classes for the tags. Few (too few) programs use tags, so try the safe_load option first to see if that works.
Please note that the recommended extension for YAML files has been .yaml since 2006.

How to get all inventory groups variables in hierarchy via Python API?

I want to collect all inventory hosts groups variables in hierarchy data struct and send them to Consul to make them available in runtime.
Calling this method - https://github.com/ansible/ansible/blob/devel/lib/ansible/inventory/manager.py#L160 I got the error
inventory.get_vars()
Traceback (most recent call last):
File "<input>", line 1, in <module>
inventory.get_vars()
File "<>/.virtualenvs/ansible27/lib/python2.7/site-packages/ansible/inventory/manager.py", line 160, in get_vars
return self._inventory.get_vars(args, kwargs)
AttributeError: 'InventoryData' object has no attribute 'get_vars'
my script
import pprint
pp = pprint.PrettyPrinter(indent=4).pprint
from ansible.parsing.dataloader import DataLoader
from ansible.vars.manager import VariableManager
from ansible.inventory.manager import InventoryManager
loader = DataLoader()
inventory = InventoryManager(loader=loader, sources='inventories/itops-vms.yml')
variable_manager = VariableManager(loader=loader, inventory=inventory)
# shows groups as well
pp(inventory.groups)
# shows dict as well with content
pp(variable_manager.get_vars())
# creates an unhandled exception
inventory.get_vars()
How to do that right way?
Python 2.7.15
ansible==2.6.2
OS Mac High Siera
The error itself seems to be caused by a bug - the get_vars method of the inventory object calls get_vars method of the InventoryData object which is not implemented.
You need to specify the group, for example:
>>> inventory.groups['all'].get_vars()
{u'my_var': u'value'}
You can create a dictionary with that data:
{g: inventory.groups[g].get_vars() for g in inventory.groups}
The above gets only the variables defined in the inventory itself (which is what the question asks about). If you wanted to get a structure with variables from group_vars, host_vars, etc. (as you indicated in your comment I want to get something similar to $ ansible-inventory -i inventories/itops-vms.yml --graph --vars you'd need to collect the data from different sources, just like Ansible does.

Error when pickling ParseResult in Python 3.5.1

I have test code that works in Python 2.7.11, but fails in Python 3.5.1:
import pyparsing as pp
import pickle
class Greeting():
def __init__(self, toks):
self.salutation = toks[0]
self.greetee = toks[1]
word = pp.Word(pp.alphas+"'.")
salutation = pp.OneOrMore(word)
comma = pp.Literal(",")
greetee = pp.OneOrMore(word)
endpunc = pp.oneOf("! ?")
greeting = salutation + pp.Suppress(comma) + greetee + pp.Suppress(endpunc)
greeting.setParseAction(Greeting)
string = 'Good morning, Miss Crabtree!'
g = greeting.parseString(string)
pkl = 'test .pkl'
pickle.dump(g, open(pkl, 'wb'))
pickle.load(open(pkl, 'rb'))
The error message is as follows:
Traceback (most recent call last):
File "C:/Users/Arne/parser/test.py", line 23, in <module>
pickle.load(open(pkl, 'rb'))
TypeError: __new__() missing 1 required positional argument: 'toklist'
__new__() refers to pyparsing.ParseResults.__new__(cls, toklist, name=None, asList=True, modal=True ).
Is it still in general possible to pickle objects returned by pyparsing in Python 3.5.1 or has something changed?
Could somebody provide a brief code sample of their use of pickle and pyparsing 2.0.7?
My real grammar takes a few minutes to parse, so I really would appreciate being able to store the results before further processing.
This fails with protocol=2 (optional 3rd arg to pickle.dump), but passes if you use pickle protocol = 0 or 1. On Python 2.7.10, 0 is the default protocol. On Python 3.5, pickle has protocols 0-4, and again, pickling ParseResults only works with protocols 0 and 1. But in Py3.5, the default protocol has changed to 3. You can work around this problem for now by specifying a protocol of 0 or 1.
More info on pickle protocols at https://docs.python.org/3/library/pickle.html?highlight=pickle#data-stream-format

AttributeError: 'DocsClient' object has no attribute 'GetDocumentListFeed'

I'm currently trying to build a Script that interacts with Google's API's, but I keep getting an Attribute error:
Traceback (most recent call last):
File "service_catalog_automation.py", line 18, in <module>
feed = client.GetDocumentListFeed()
AttributeError: 'DocsClient' object has no attribute 'GetDocumentListFeed'
This is the code I'm trying to use
import gdata.gauth
import gdata.docs.client
import sys
def sys_print(text):
sys.stdout.write(str(text))
sys.stdout.flush()
CONSUMER_KEY = 'domain.com'
CONSUMER_SECRET = 'abcde1234'
requestor_id = 'myuser#domain.com'
client = gdata.docs.client.DocsClient(source='my-script-v1')
client.auth_token = gdata.gauth.TwoLeggedOAuthHmacToken(
CONSUMER_KEY, CONSUMER_SECRET, requestor_id)
# Retrieve user's list of Google Docs
feed = client.GetDocumentListFeed()
for entry in feed.entry:
sys_print(entry.title.text)
sys_print('\n')
I've pulled client.getDocumentListFeed() portion of code from their sample code here and have even tried a bare minimum approach using their sample code under the 2LeggedOAuth section here. (I also have tried GetDocList() from that example with the same error)
I have downloaded Google's gdata-python-client to a linux vm's home directory and installed it by running python setup.py install and ran the test python all_tests.py with no errors.
Any help would be greatly apperciated
In the first example, they're assigning their client object as the return of gdata.docs.service.DocsService().
The second example also returns the client object as a DocsService type:
client = gdata.docs.service.DocsService(source='yourCompany-YourAppName-v1')
This would seem to imply that gd_client is of type DocsService, not DocsClient

GitPython Clone repository error

I want to clone git repository with parameters (--recursive, -b <branch>) but I get the following error.
Traceback (most recent call last):
File "./git-clone.py", line 15, in <module>
r = git.Repo.clone(repo_dir, b=branch, recursive=git_url)
TypeError: unbound method clone() must be called with Repo instance as first argument (got str instance instead)
Here is my code:
#!/usr/bin/env python
import git
import os
import shutil
git_url = "<url>..."
repo_dir = "/home_local/user/git-repository"
branch = "branch"
if os.path.exists(repo_dir):
shutil.rmtree(repo_dir)
r = git.Repo.clone(repo_dir, b=branch, recursive=git_url)
If I replace git.Repo.clone with git.Repo.clone_from its working fine but this command not accept my parameters.
try:
r = git.Repo.clone_from(git_url, repo_dir, branch=branch, recursive=True)
The first argument, is where you cloning from (the remote repository). The second argument is where you want to store the clone. All other arguments are passed on to git-clone command. eg --branch="branch" and --recursive. You should probably stick to the long argument names rather than the abbreviations. Since the recursive flag is either present or not, it's values can only be True or False.

Categories

Resources