Python Daily
2.57K subscribers
1.48K photos
53 videos
2 files
38.9K links
Daily Python News
Question, Tips and Tricks, Best Practices on Python Programming Language
Find more reddit channels over at @r_channels
Download Telegram
[AF][flask-mail]Recursion error on flask-mail connection attempt

I'm getting a recursion error when I attempt to connect to my hMail server that I set up for testing purposes. Both my Flask server and hMail server are on the same computer at 127.0.0.1. The traceback is as follows:

Traceback (most recent call last):
File "<console>", line 1, in <module>
File "C:\Users\LaNoktaTempesto\myapp\helpers.py", line 47, in send_notice
self.send_message(message)
File "c:\users\LaNoktaTempesto\appdata\local\programs\python\python36-32\lib\site-packages\flask_mail.py", line 502, in send_message
self.send(Message(*args, **kwargs))
File "c:\users\LaNoktaTempesto\appdata\local\programs\python\python36-32\lib\site-packages\flask_mail.py", line 491, in send
with self.connect() as connection:
File "c:\users\LaNoktaTempesto\appdata\local\programs\python\python36-32\lib\site-packages\flask_mail.py", line 506, in connect
app = getattr(self, "app", None) or current_app
File "c:\users\LaNoktaTempesto\appdata\local\programs\python\python36-32\lib\site-packages\flask_mail.py", line 574, in __getatt
r__
[Previous line repeated 319 more times]
RecursionError: maximum recursion depth exceeded

I don't seem to be finding anyone else having this problem, and as I am a beginner, I'm not sure where to start debugging this issue.

/r/flask
https://redd.it/7009m2
How to change directory inside a notebook ?

My project directory typically look like this :

home/
├── data/
│ ├── some.csv
│ ├── another.csv
│ └── etc.csv
├── notebook/
│ ├── myproject.ipynb
│ └── testing.ipynb
└── src/
└── script.py

I have set my configuration to launch my notebook in the home directory. Although I am having trouble properly loading files from the data directory.

My big challenge is that I am using Windows, Linux and Mac. I've used the OS module but my code doesn't work will all OS.
I can't find the universal code that would allow me to move from the notebook to the data.

Any help ?

/r/IPython
https://redd.it/702c9h
Best practices for group projects?

For uni we have to do a ML group project in Jupyter. Putting together our code manually is quite a pain in the ass. However, git has a lot of conflict problems when cells are added/deleted which take even more time to solve than manually putting together the code.

What are your workflows in group projects?

Thanks so much!

/r/IPython
https://redd.it/702spv
Can Python check if some security cameras have been covered or saturated with artificial lights?

Hi everyone,
I'm working on a Debian server (version 8) that captures some h.264 streams from security cameras with ffmpeg, and stores them in chunks of 1 minute long mp4 videos.
My task is the following: Check if the cameras have been covered (have gone dark), or some light have been pointed directly into the camera so it's saturated (completely white).

The "dark check" has to keep into account that on the stream there are some white timestamps, a logo and some noise, so it's never completely dark, even if you paint over the camera lens with a spry can.

I was thinking about this solution:

1) For every minute long video, extract the middle frame with ffmpeg into a jpg file
2) I analyze the jpg file with python to extract the average brightness.
3) If the brightness is close to "0" (8 bit black) or "255" (8 bit white) it will trigger a warning

The server has about 40 cameras recording 24/7 and of course the computational load of the "dark" and "white" check must be as low as possible.

Do you think my approach is reasonable? Could I do this with other tools?

I was thinking about OpenCV to extract the frame instead of ffmpeg to keep everything in RAM but I don't really know the library.

Thanks in advance.

/r/Python
https://redd.it/70366e
Are there any up to date React + Django integration tutorials?

Hey there,

I'm looking to go SPA with my django site using React. I have the API set up using DRF.

I started using [the excellent tutorial that /u/gammadog posted here back in February,](https://www.reddit.com/r/django/comments/43y4wd/a_tutorial_on_using_django_rest_framework_with/), but it looks like React's syntax has been updated quite a lot since then and his tutorial is no longer effective. I'm doing my best to bushwhack a path using documentation and other online tutorials. However, I am consistently hitting very frustrating roadblocks.

Do any of you know a good, up to date tutorial to get all my pieces communicating with one another effectively?

Thanks!

/r/django
https://redd.it/704mno
Django on Raspberry Pi, interacting with sensor network and alerts

I'm working on a system to monitor some sensors and provide feedback on certain conditions. Normally I'd just write something like this in C++ or python because it's straightforward and not complicated, but we've decided to integrate a display for constant readout. It seems to make sense to use Django for the datastore and the display portions, something that Django is awesome at. Since all the sensors will be available across an ethernet, I'll just make a simple REST api for reporting updates and then use django/javascript to show system status on a display.

Does anyone have any pointers or suggestions for the above setup? I'll be writing a python system utility that will run and keep track of everything and interact with the database, probably through management commands. Or is interacting over REST a better option?

If I'm pulling in about 300 readings per minute, do I need to worry about purging data over time, or will postgres be ok for this? Historical data (>10 days) is not super important for this system.

Anyone else have feedback or advice on deploying django on rpi? It's on a local network and will basically just have a small handful of users.

Thanks!

/r/django
https://redd.it/703mh2
Help tying up loose ends on my payment system integration (Stripe/Django) [LONG]

**OMG;TIWTL;DR;FU (OMG, this is way too long, didn't read, F you): Can someone please help me work through the design/architecture of my payment system with Stripe? I think I have most of the pieces to the puzzle but I'm not sure how they fit together.**

-----------------------------

**Long Version:**

Hi there,

I'm going to try to make this as detailed but also as concise as possible. I am trying to tie up the loose ends on the Stripe subscription/recurring payment system for my web app before taking it live and I think I just need some help understanding exactly what the whole process should look like from a bird's eye view. If you've successfully integrated Stripe with Django before it will definitely help a lot in understanding the questions I have and your ability to provide answers. Ok, so here we go!

My current start to finish workflow from new user registration to payment is as follows, and this all works (though there are likely unforeseen bugs and holes in it, so please feel free to call them out):


**User Registration:**

- User uses standard django-registration form to register, an activation email is sent, and user clicks link in activation email to activate their account.
- Upon new user registration the following happens:
- A new user profile is created including a few extra fields that extend the default user model through a OneToOneField link
- A user PlanProfile is created, initializing them to the free plan, initializing the boolean premium_user field to False, and giving them a default "NA" value for stripeID and subscriptionID (These will be the stripe customer ID and subscription ID once they join a paid plan. Without a paid plan there is no need for them)

**Joining a Paid Plan:**

- Customer navigates to /plans/ and selects a payment plan
- Depending on which button they click (simple pricing tables), the appropriate StripeCheckout handler is configured and called via Javascript, and the form data (CC token, email address, name, plan type, etc) is submitted to the payment processing view /plans/processpayment/
- Payment processing view checks the user's plan profile to see if they have an existing Stripe customer ID **and** existing Stripe subscription ID. They **should** have both, or neither (at this point).
- If they do **NOT** have both (i.e. they are a new customer):
- A new Stripe customer is created, assuming there are no card/form validation errors
- A new subscription for that customer is created (assuming same)
- Assuming no errors from Stripe:
- The user's plan profile is updated, setting the premium_user field to True, and the stripeID and subID fields to the customer and subscription IDs returned from Stripe, respectively.
- The user is redirected to the post payment view saying thanks, reflecting their new subscription, confirmation email is sent from Stripe with a copy of their invoice
- If they **DO** have both (i.e. they are already a customer):
- Payment processing view uses the subscription ID from the user's plan profile to get the customer ID from that Stripe subscription within Stripe
- Payment processing view uses the customer ID from the user's plan profile to get the subscription ID from that Stripe customer object within Stripe
- Payment processing view compares the customer ID and subscription ID returned in the Stripe objects with the local customer and subscription IDs and makes sure they are the same (this confirms that we're working with the right customer and the right subscription, I think!), and makes sure that the customer has an active subscription by checking that the length of the subscriptions.data list is not 0.
- If **ANY** of the above is not true (e.g. customer doesn't have an active sub, or there is a conflict between the Stripe object IDs and the local customer/sub IDs):
- FAIL and return the user to the post payment view with an error that something is wro
ng. Make no changes to the account.
- If **ALL** of the above are true:
- Retrieve the customer from Stripe using the local customer ID
- Update the customer's name, email, and CC token with the new data from the form
- Create a new subscription with the new plan the user is signing up for and save the ID of it returned from Stripe, assuming no errors on the Stripe end (card errors, etc)
- Delete the customer's old subscription in Stripe, using the local subscription ID to look it up
- Again, assuming no errors from Stripe:
- The user's plan profile is updated, setting the premium_user field to True, and the stripeID and subID fields to the customer and subscription IDs (in this case the one we saved earlier) returned from Stripe, respectively.
- The user is redirected to the post payment view saying thanks, reflecting their new subscription, confirmation email is sent from Stripe with a copy of their invoice

So it **seems** like that all works. However, all that does is handle creating new subscriptions and (sort of) changing plans. The reason I say (sort of) with changing plans is that the changing plan logic is not good because it doesn't handle proration/refunds if a customer cancels in the middle of a plan period, price changes between plans, or if a customer just wants to cancel their plan entirely and go back to free. So I decided what I think I should do is, if a customer wants to change plans, they must first cancel their current one and then join the new one. This would essentially force them to delete their subscription, get a refund, wipe out both their local AND remote Stripe customer and subscription IDs, and then start fresh as a "new" customer. So on that venture, I decided to write a payment cancellation view. This is where I'm getting sort of lost.

It's easy enough to just pull the local customer ID from the database, confirm that it returns a valid Stripe object, get the subscription ID out of that object and then delete that subscription. Upon doing that a customer.subscription.deleted webhook event would be sent to my webhook view (which also works, BTW, though it doesn't currently do anything) which I feel like I can use somehow. Also, when I create a new subscription, or when a new charge is made on an existing subscription, there are various webhooks sent such as (in no particular order):

- customer.created
- customer.card.created
- charge.succeeded (or failed)
- invoice.created
- invoice.payment_succeeded (or failed)
- customer.subscription.created
- customer.updated
- *probably others along the way*

So if you've made it this far, I guess what I want to know is, how the hell do I tie this all together? I feel like I have the right pieces to the puzzle, and I've assembled the border, but the middle pieces are still all strewn about the table waiting to be put together. I think it would be best if we just leave the discussion open-ended rather than asking specific questions since it's tough for me to come up with any specific questions without really knowing what I need to know. So I guess I am looking for some discussion/help with how to structure the whole process. For example:

- How can I handle an existing subscription that gets charged the regular monthly payment? What should I do in the database?
- How should I handle cancelled subscriptions? Calculate refunds via webhooks and then issue a refund via the cancellation view, or code the webhook view to issue a refund whenever it receives a subscription.deleted event, or something else?
- Should my payment processing view interact with my webhooks at all, and if so, how? In other words does the error handling I'm doign already seem ok, or should I be using the webhooks to verify that stuff happened correctly on the Stripe end before making changes to the database, and if so, how can I halt the payment processing view along the way and check that various webhooks are coming in as expected? The webhook view is sort of an event listener so cu
rrently it doesn't have any interaction with the payment processing view.

I'm sure more questions will come up along the way but if anyone has dealt with building this type of integration before I would greatly appreciate some guidance! I'm not really looking for specific answers with code but more of a general answer to the question "how the hell should all of this fit together so I don't destroy my database or charge my customers incorrectly?"

Whew, ok I'm done now. Let me know if you have any questions and THANK YOU!!!!

/r/django
https://redd.it/705dwa
Having some trouble with OCR, excel, scanned documents, and Adobe acrobat. Was directed here from r/excel.

We have these stacks of "tickets" (there are thousands of them) that are printed similarly to a receipt at a gas station. Sometimes there's handwriting in there between printed text that screws up the character recognition in Adobe acrobat pro. Adobe seems to have trouble even deciphering the printed ink text that is perfect.

The problem gets worse, the tickets aren't printed in an excel friendly format really. The are all the same format though and they are batch scanned in so theres probably 40-50 at least in each batch (pdf).

I need all this data in excel so I can check it for errors and discrepancies. I don't know the best way to do this (they were entering by hand before). Do I need a vba macro in excel or JavaScript in adobe? I think the best thing to do is to try to get the extract data I need from the specific areas of each type data only instead of using OCR on the whole ticket.

I also exported the whole tickets to Word and the OCR seemed to work better for that transfer for some reason. I thought it might be easier to transfer the data from word to excel instead of acrobat to excel.

If there are any other subs to post this to that might be helpful, I'd like to know. If I don't figure a way to do this soon I'll have type each ticket into excel by hand. Ill die from inefficiency.

I'm not an experienced programmer so something very complecated will not be easy for me. Thanks in advance.

Original post below.

https://www.reddit.com/r/excel/comments/7076lh/i_need_to_extract_data_from_scanned_pdfs_in_to

/r/Python
https://redd.it/707bxb
PSA - Malicious software libraries in the official Python package repository (xpost /r/netsec)
http://www.nbu.gov.sk/skcsirt-sa-20170909-pypi/

/r/Python
https://redd.it/709vch
Anyone have a high res version of the XKCD Python Antigravity Image?

Randall Munroe is giving a talk here in a couple of weeks and I'd like to get him to sign a copy.

https://xkcd.com/353/

I can't see it available to buy in the XKCD store. Can anyone please link me to a high res version?



/r/Python
https://redd.it/70a9n8