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
Script to add bookmarks to a PDF (Need review and suggestions)

Hello guys,

So i made this little script that adds bookmarks to PDFs using Py2PDF module. It's not really a full automation of the process of adding bookmarks since you still have to manually add the parent bookmark and add the bookmarks (Chapters + their respectives pages) into a dictionnary. Once the dictionnary is made, the bookmarks are added with a for loop that uses the addBookmark method in a new output PDF file.

from PyPDF2 import PdfFileWriter, PdfFileReader

pdf_object = open("pdf_without_bookmarks.pdf","rb") #rb stands for read binary
output = PdfFileWriter()
input = PdfFileReader(pdf_object)

input_numpages = input.getNumPages()

#basically just copy the input file
for i in range(input_numpages):
output.addPage(input.getPage(i)) #insert page in the output file

parent_1 = output.addBookmark('Parent 2', page of parent 2) # add parent bookmark
bookmarks_dic_1 = {
"Chapter 1": page of Chapter 1,
"Chapter 2": page of Chapter 2,
"Chapter n": page of Chapter n,
}



for k, v in bookmarks_dic_1.items():

output.addBookmark(k ,v +1,parent_1 ) #add child bookmarks


parent_2 = output.addBookmark('Parent 2', page of parent 2)
bookmarks_dic_2 = {
"Chapter 1": page of Chapter 1,
"Chapter 2": page of Chapter 2,
"Chapter n": page of Chapter n,
}

for k, v in bookmarks_dic_2.items():

output.addBookmark(k ,v +1,parent_2 ) #add child bookmarks

outputstream = open('pdf_with_bookmarks.pdf','wb') #creating result
output.write(outputstream) #writing to result pdf
outputstream.close() #closing result


Initially, when i started working on this, i wanted to scrape Chapter and Page information from PDF filesummary, but i realized it was too ambitious and that it requires to know how to use regex (Am i right?).

I also want to know if there's a way to put all this in one loop, instead of having to create a new loop for each parent. But i don't know how. Like is there such thing as a dictionnary with more than 1 value? The Parent will be the key and the values Name of chapter and Page of chapter. A kind of a double dictionnary. Something like this :

summary_dictionnary = {"Parent 1":{"Chapter 1": Page, "Chapter n": Page}, "Parent n": {"Chapter 1": Page,"Chapter n":Page}}

Is that even possible?
I'm also posting here to have a general review on my code, what do you guys would change?

Big thanks in advance

/r/Python
https://redd.it/7tih40
Error in documentation

Hi,

I am a beginner. I was going through the getting started documentation and found some typos and error.

[https://docs.djangoproject.com/en/2.1/intro/tutorial03/#writing-more-views](https://docs.djangoproject.com/en/2.1/intro/tutorial03/#writing-more-views)

def detail(request, question_id):
return HttpResponse("You're looking at question %s." % question_id)

def results(request, question_id):
response = "You're looking at the results of question %s."
return HttpResponse(response % question_id)

def vote(request, question_id):
return HttpResponse("You're voting on question %s." % question_id)

Gives the following error:

return HttpResponse("You're voting on question %s", % question_id)
SyntaxError: invalid syntax

To solve the problem I performed

*return* HttpResponse("You're voting on question %s", question\_id) #excluded the '%' prefixed to question\_id

​

I am using Linux python3.7 django 2.1.5.

Will the changes be made or should I make an official report on their site?

If so, How should I sent the error to the django team.

/r/django
https://redd.it/ano2kr
How do I decide on model validation vs. form validation?

[https://docs.djangoproject.com/en/2.2/ref/validators/#writing-validators](https://docs.djangoproject.com/en/2.2/ref/validators/#writing-validators)

What must be taken into account when deciding to apply a validator on either a form or on a model? As the documentation says, a Django form attempts to convert user supplied data into Python data types before sending the data to any supplied validators.

That alone makes me thing form validation is better suited for this. How else can I go about thinking about this generally?

/r/djangolearning
https://redd.it/d5ssoq
FastStream: a powerful and easy-to-use library for building asynchronous services with event streams such as ApacheKafka and RabbitMQ

**FastStream** simplifies the process of writing producers and consumers for message queues, handling all the parsing, networking and documentation generation automatically. It is a new package based on the ideas and experiences gained from **FastKafka** and **Propan**. By joining our forces, we picked up the best from both packages and created a unified way to write services capable of processing streamed data regardless of the underlying protocol. We'll continue to maintain both packages, but new development will be in this project.

Making streaming microservices has never been easier. Designed with junior developers in mind, **FastStream** simplifies your work while keeping the door open for more advanced use cases. Here's a look at the core features that make **FastStream** a go-to framework for modern, data-centric microservices.

Multiple Brokers: FastStream provides a unified API to work across multiple message brokers (Apache Kafka, RabbitMQ support)
**Pydantic Validation**: Leverage **Pydantic's** validation capabilities to serialize and validate incoming messages
[Automatic Docs](https://github.com/airtai/faststream#project-documentation): Stay ahead with automatic [AsyncAPI](https://www.asyncapi.com/) documentation
Intuitive: Full-typed editor support makes your development experience smooth, catching errors before they reach runtime
[Powerful Dependency Injection System](https://github.com/airtai/faststream#dependencies): Manage your service dependencies efficiently with FastStream's built-in DI system
**Testable**: Supports in-memory tests, making your CI/CD pipeline faster and

/r/Python
https://redd.it/16pc38l