Capturing errors on Stripe source creation - stripe-payments

I'm running the following code, which is failing
stripe_card_response = customer.sources.create(source=source)
The response that I get back is as follows:
File "/opt/python/run/venv/local/lib/python3.6/site-packages/stripe/stripe_object.py", line 50, in __getattr__
raise AttributeError(*err.args)
AttributeError: sources
Within my python code, I would I capture that error associated with this failure so that I can print it?
Thanks!

I think you might be using an older syntax that is no longer supported. To attach a source to a Customer you'd do:
customer = stripe.Customer.modify(customer.id, source=source)

Related

python problem creating file and using file by its name only like 'sample.txt' not by giving it path (using v s code)

enter image description here
every time i try using the file in python by using it name like 'sample.txt' terminal shows
no such file or directory
for solving it I had to provide the file path to it then only it works
solve the problem and provide me some steps by which I can surely avoid this problem next time
and read the terminal and provide the reason for it also it is showing
AttributeError: 'str' object has no attribute 'write'
please help me with this too.
thanks
Its necessary to open the file to print to it, and close it after. You will get the attribute error if you try to output to the file rather than the file object.
You can set the path and name of the file when you open it. See example below
demofile = open(r'c:\Temp\demofile.txt', 'w')
print(1,5,3,25,34, sep='->',end='!',file=demofile)
demofile.close()
Or using context manager to handle close
with open(r'c:\Temp\demofile.txt', 'w') as demofile:
print(1, 5, 3, 25, 34, sep='->', end='!', file=demofile)

Error with adding text components in psychopy procedure

I have downloaded the procedure I have on my laptop on my lab’s computer from my Google Drive, and I had to take some edits to test it.In order to this, I have to add some text components with some specific code. However when I tried to add and use the $ symbol to read the text I would like to refer to, I get the following error:
Traceback (most recent call last):
File "/usr/local/lib/python3.8/dist-packages/psychopy/app/builder/dialogs/paramCtrls.py", line 31, in validate
validate(self, self.valType)
File "/usr/local/lib/python3.8/dist-packages/psychopy/app/builder/dialogs/paramCtrls.py", line 550, in validate
val = str(obj.GetValue())
RecursionError: maximum recursion depth exceeded while calling a Python object
Could anyone possibly have some good piece of advice or workaround to manage with it?
Thanks
What version of PsychoPy are you using? I believe this was fixed in Feb 2022 so probably updating to a new version of psychopy should fix it for you.
https://github.com/psychopy/psychopy/pull/4569

Discord py Attachments

I am creating a bot that creates a backup of discord guilds in mysql. (Basically it allows me to make a website that is an extension of the discord channel.)
I am going to be downloading and saving attachments in then (guild,channel,user) folder path so I can query them on the website.
Also I am going to be purging inactive users by checking to see who is active withing the last 24 hours, last week, etc.
Anyway so I am trying to check to see if a message has an attachment using the following script,
#client.event
def on_message(message):
msg = str(message.content)
channel = str(message.channel)
guild = str(message.guild)
author=str(message.author)
if checkfordb(guild) == 1:
if checkfortable(channel,guild) == 1:
addmsg(channel,guild,msg,author)
else:
createtb(channel,guild)
addmsg(channel,guild,msg,author)
else:
createdb(guild)
createtb(channel,guild)
addmsg(channel,guild,msg,author)
if message.Attachment.size > 0:
print("There is an attachment")
else:
print("There is no attachment")
The problem is that when I run this script I get the following error,
Ignoring exception in on_message
Traceback (most recent call last):
File "/usr/local/lib/python3.6/dist-packages/discord/client.py", line 312, in _run_event
await coro(*args, **kwargs)
File "bot2.py", line 145, in on_message
if message.Attachment.size > 0:
AttributeError: 'Message' object has no attribute 'Attachment'
So I checked the documentation and sure enough despite tons of other people saying they are using it there is no attachment attribute for message. The documentation under attachment says you can use class discord.attachment. So I looked at the classes and I see no way to pull an attachment when it is posted. Does anyone have any ideas on how to do this?
Any and all help is appreciated.
The correct attribute name is Message.attachments as Patrick Haugh suggested. You could use the len() function on that to get the "size" of it since it's just a normal python list of discord.Attachment objects.
if message.attachments:
Simple as that!

How to add a new method to an existing import in python? Specifically moviepy

For whatever reason, Python is not allowing me to access a custom method I created in moviepy's preview.py file. I just want to know how to correctly implement it into the file. For reference, before I changed the name of the method, it was working correctly.
I checked at least two __init.py__ files and they were effectively empty. I couldn't find if methods are initialized anywhere, and is probably what I'm missing.
I also tried restarting Git Bash and that didn't work either (another solution I saw).
Original:
#convert_masks_to_RGB
def preview(clip, fps=15, audio=True, audio_fps=22050, audio_buffersize=3000,
audio_nbytes=2, fullscreen=False):
Changed:
#requires_duration
#convert_masks_to_RGB
def preview_custom(clip, marker_overlay="marker_overlay.png", fps=15, audio=True, audio_fps=22050, audio_buffersize=3000,
audio_nbytes=2, fullscreen=False):
There are more than a few differences between the changed and original method, however at the moment the only result I expect is having the method be called correctly. Error is below:
Traceback (most recent call last):
File "T3AJM.py", line 249, in <module>
main()
File "T3AJM.py", line 34, in main
GUI_main_menu()
File "T3AJM.py", line 85, in GUI_main_menu
GUI_play_markers()
File "T3AJM.py", line 125, in GUI_play_markers
video.preview_custom(marker_overlay=TEMP_OVERLAY_FILE)
AttributeError: 'VideoFileClip' object has no attribute 'preview_custom'
Thank you for your time.
I'm not even sure if this technically fixes the problem, but just doing:
from moviepy.video.io.preview import *
and
preview_custom(video, marker_overlay=TEMP_OVERLAY_FILE)
fixed the problem. I have no idea why I had to change the way it was called, as doing clip.preview(), or in this case video.preview() worked perfectly fine before, but whatever.

Error handling netCDF file in Python

I am extracting data from netCDF files with Python code. I need to check if the netCDF files are in agreement with the CORDEX standards (CORDEX is a coordinated effort to carry modelling experiments with regional climate models). For this I need to access an attribute of the netCDF file. If the attribute is not found, then the code should go to the next file.
A snipet of my code is as follows:
import netCDF4
cdf_dataset = netCDF4.Dataset(file_2read)
try:
cdf_domain = cdf_dataset.CORDEX_domain
print(cdf_domain)
except:
print('No CORDEX domain found. Will exit')
....some more code....
When the attribute "CORDEX_domain" is available everything is fine. If the attribute is not available then the following exception is raised.
AttributeError: NetCDF: Attribute not found
This is a third party exception, which I would say should be handled as a general one, but it is not, as I am not able to get my "print" inside the "except" statement to work or anything else for that matter. Can anyone point me the way to handle this? Thanks.
There is no need for a try/except block; netCDF4.Dataset has a method ncattrs which returns all global attributes, you can test if the required attribute is in there. For example:
if 'CORDEX_domain' in cdf_dataset.ncattrs():
do_something()
You can do the same to test if (for example) a required variable is present:
if 'some_var_name' in cdf_dataset.variables:
do_something_else()
p.s.: "catch alls" are usually a bad idea..., e.g. Python: about catching ANY exception
EDIT:
You can do the same for variable attributes, e.g.:
var = cdf_dataset.variables['some_var_name']
if 'some_attribute' in var.ncattrs():
do_something_completely_else()

Resources