How to execute twitter.Api.PostUpdate in loop? - python

This code executes with error:
# some constants and auth before, looks not important
topPosts = reddit.get_subreddit('funny').get_top(limit=3)
for post in topPosts:
twitter.PostUpdate(status = post.title, media = post.url)
Console log:
Traceback (most recent call last):
File "script.py", line 17, in <module>
twitter.PostUpdate(status = post.title, media = post.url)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/twitter/api.py", line 990, in PostUpdate
media_additional_owners)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/twitter/api.py", line 1132, in UploadMediaChunked
boundary = bytes("--{0}".format(uuid4()), 'utf-8')
TypeError: str() takes at most 1 argument (2 given)
If I do just post.label in loop it works perfectly.
If I execute only one (w/o loop) PostUpdate it works perfectly.
I think it's happening because PostUpdate is asynchronous, but can't figure out how to fix it. Please help.

This is a bug in python-twitter library and it's fixed in this PR. The problem is that bytes in python2 equals to str and accepts only one argument while in python3 bytes requires encoding as a second argument.

Related

Use simplify from Mathics in Python

I'm trying to use the simplify methods from Mathics in my Python program. The problem is, even I read the documentation of Mathics (https://mathics-development-guide.readthedocs.io/en/latest/extending/developing-code/calling-mathics-library-from-within-python-code.html) , I can't figure out how to use the "Simplify" method. I also tried to use the exemple one their documentation :
# This is the same as before
from mathics.session import MathicsSession
session = MathicsSession(catch_interrupt=True)
# These are Mathics classes we are going to use.
from mathics.core.expression import Expression, Integer
# Compute 20!
x = Expression("Factorial", Integer(10)
).evaluate(session.evaluation).to_python()
print(x) # 2432902008176640000
But it raised an error :
Traceback (most recent call last):
File "c:\Users\ASUS TUF\Documents\ESEO\Stage
Technique\Dev\geneticSolver\mathics_test.py", line 10, in <module>
).evaluate(session.evaluation).to_python()
File "C:\Users\ASUS TUF\AppData\Local\Programs\Python\Python310\lib\site-
packages\mathics\core\expression.py", line 441, in evaluate
names.add(expr.get_lookup_name())
File "C:\Users\ASUS TUF\AppData\Local\Programs\Python\Python310\lib\site-
packages\mathics\core\expression.py", line 636, in get_lookup_name
lookup_symbol = lookup_symbol._head
AttributeError: 'str' object has no attribute '_head'
Does anyone has already worked with Mathics on Python and can explain me how to deal with it ?

cassandra-driver issue on shutdown

Using cassandra-driver version 3.24.0, I'm able to connect to a cluster and run queries (using protocol 4) without any issues.
However when I call cluster.shutdown(), or let the script completes which calls it automatically I think, I get the following exception :
Error in atexit._run_exitfuncs:
Traceback (most recent call last):
File "cassandra\cluster.py", line 221, in cassandra.cluster._shutdown_clusters
File "cassandra\cluster.py", line 1759, in cassandra.cluster.Cluster.shutdown
File "cassandra\cluster.py", line 3147, in cassandra.cluster.Session.shutdown
File "cassandra\pool.py", line 493, in cassandra.pool.HostConnection.shutdown
File "C:\Users\myuser\AppData\Local\Programs\Python\Python37\lib\site-packages\cassandra\io\asyncorereactor.py", line 386, in close
ConnectionShutdown("Connection to %s was closed" % self.endpoint))
File "cassandra\connection.py", line 207, in cassandra.connection.DefaultEndPoint.__str__
TypeError: %d format: a number is required, not str
I couldn't find anything about this, it looks like an issue in the library itself but I assume I must be passing something it doesn't like somewhere.
Any ideas what could be causing this ? Weather I use execution profiles or the legacy API, same result.
Thanks
Have the same issue.
Found that in cassandra/connection.py in row 207 there is str method, which uses port as %d. All you need is during cluster creation send int, not str
cluster = Cluster(contact_points=[hostname], port=int(port), execution_profiles=profiles, auth_provider=auth)
I faced the same issue. I type cast the port no to int value.
int(port) and issue is resolved.

TypeError: initial_value must be unicode or None, not str,

I am using SOAPpy for soap wsdl services. I am following this toturail. My code is as follow
from SOAPpy import WSDL
wsdlfile = 'http://track.tcs.com.pk/trackingaccount/track.asmx?WSDL'
server = WSDL.Proxy(wsdlfile)
I am getting this error on the last line of my code
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/home/adil/Code/mezino/RoyalTag/royalenv/local/lib/python2.7/site-packages/SOAPpy/WSDL.py", line 85, in __init__
self.wsdl = reader.loadFromString(str(wsdlsource))
File "/home/adil/Code/mezino/RoyalTag/royalenv/local/lib/python2.7/site-packages/wstools/WSDLTools.py", line 52, in loadFromString
return self.loadFromStream(StringIO(data))
TypeError: initial_value must be unicode or None, not str
I tried to convert the string into utf using
wsdlFile = unicode('http://track.tcs.com.pk/trackingaccount/track.asmx?WSDL, "utf-8")
but still having same error. What is missing here ?
I just ran into this problem with some very old 2.7 code that no longer worked due to the TLS update. After updating to the most recent version of Python 2 I ended up getting this issue.
I was only able to fix this by setting up a new virtual environment, then modifying the wstools package in that virtual environment to use BytesIO instead of StringIO.
Replace every required instance of StringIO. For example:
# WSDLTools.py
...
from IO import BytesIO
...
return self.loadFromStream(BytesIO(data))
Not ideal, but it worked. Easier than migrating everything to Python 3...

Python Dropbox API - Invalid "cursor" parameter

I've been migrating some Python 2.7.11 code to 3.5.1 after running into trouble with unicode. This was the last straw - since I started using the venv module there's no reason to be on 2.7 just because someone doesn't like 3!
The problem occurs while trying to run a one-way sync (ie. downloading changes only).
Here is the full error message, paths shortened:
Traceback (most recent call last):
File "%SCRIPT%.py", line 209, in <module>
updated_schedules = dbx_sync.One_Way_Sync(config['Dropbox Parameters']['Directory'], config['Dropbox Parameters']['Base Path'])
File "%COMMON_PATH%\modules\dropbox_sync_schedules.py", line 62, in One_Way_Sync
result = client.delta(cursor, base_path)
File "%COMMON_PATH%\env-home\lib\site-packages\dropbox\client.py", line 569, in delta
return self.rest_client.POST(url, params, headers)
File "%COMMON_PATH%\env-home\lib\site-packages\dropbox\rest.py", line 322, in POST
return cls.IMPL.POST(*n, **kw)
File "%COMMON_PATH%\env-home\lib\site-packages\dropbox\rest.py", line 260, in POST
is_json_request=is_json_request)
File "%COMMON_PATH%\env-home\lib\site-packages\dropbox\rest.py", line 235, in request
raise ErrorResponse(r, r.read())
dropbox.rest.ErrorResponse: [400] 'Invalid "cursor" parameter: u"b\'\'"'
Searching for "invalid cursor parameter" wasn't any help, so I thought I'd come here.
u"b\'\'" is the key here. I just couldn't understand how that representation had ended up being sent as a string.
The issue was in reading the old cursor from a file (which for this example is empty): in Python 2 I had opened the file in mode rb - in Python 3 just r is all that's required, and everything works.
Hurrah!

PyPDF2 TypeError when trying to run example from lib

I've got PyPDF2 lib from here:
https://github.com/mstamy2/PyPDF2/tree/Python3-3
When trying to run script "Example 1:" from from there see it:
PyPDF2 python versions (2.5 - 3.3) compatibility branch
Traceback (most recent call last):
File "1.py", line 6, in <module>
input1 = PdfFileReader(open("document1.pdf", "rb"))
File "C:\Python33\lib\site-packages\PyPDF2\pdf.py", line 595, in __init__
self.read(stream)
File "C:\Python33\lib\site-packages\PyPDF2\pdf.py", line 1097, in read
streamData = StringIO(xrefstream.getData())
TypeError: initial_value must be str or None, not bytes
What is wrong?
It was a problem related to the compatibility within PyPDF2 and Python 3.
In my case, I have solved it by replacing pdf.py and utils.py with the ones you will find here, where they basically control if you are running Python 3 and, in case you are, receive data as bytes instead of strings.

Categories

Resources