justinlillico.com

Jun 24, 2019

The F-String

Image credit Another way to concatenate strings. Hi guys, sorry it's been a fortnight! I've been hella busy and still am so this is going to be short. So I recently discovered the f-string in python. a_string = " are pretty damned cool!" print("f-strings" + a_string) So I'm sure most of us are familiar with the above for good old string concatenation. If you are super tricky, you might even know� a_string = " are pretty damned cool!" print("f-strings{0}").format(a_string) These are both well and good, but I have recently learned of a method i enjoy more than both of these. a_string = " are pretty damned cool!" print(f"f-strings{a_string}") Simple. Concise. Beautiful. Stay tuned for next week!

Jun 10, 2019

Pure Python HTML to PDF

Image credit Hello again! Welcome to another Monday and another short one for you here. So, have you ever needed to make a PDF file programmatically? I know I have. One of the main advantages of having a pdf as opposed to some other graphical format is being able to print them from the right click (context) menu. The simplest method of programmatically styling something like this in my experience seems to be HTML. Once you have this, simply use some kind of library to convert it into a PDF. Right? Well, kind of. You see, another nuance to this thing here is that generally speaking, you want your applications to be independent of the OS and environment. Well, you might wanna have Python and a couple of packages installed, but that�s it. Let�s not rely on the user to install anything external. Let me introduce xhtml2pdf As I was unable to find any clear cut instructions on how to do this locally, so let me save you some trouble and walk you through it. from xhtml2pdf import pisa from jinja2 import Environment, FileSystemLoader First, let's import the pisa module from the xhtml2pdf package. Optionally, you can import the Environment and FileSystemLoader modules from jinja2 if you would like to populate a html template with some data. Then, provided you want to use Jinja2� environment = Environment(loader=FileSystemLoader(searchpath="./")) template = environment.get_template('./Template.html') We create an environment specifying we want to load from the filesystem and that the place to load from is the current directory. output_from_parsed_template = template.render(**kwargs) Above, we are grabbing the properly formatted jinja template (with {{ variables }} and the like), and populating it with data. If you want to know more about that, google it. It's not the focus of what I'm talking about here. resultFile = open(outputFilename, "w+b") pisaStatus = pisa.CreatePDF(output_from_parsed_template, dest="./output_pdf") resultFile.close() In the above, we open some kind of output file for writing as binary. Then, we use the CreatePDF function from pisa to do the magic. Pure, pythonic conversion. Then of course, we tidy up and close the file. That's all there is too it folks! Hope that was helpful to someone! See ya'll next week :)

Jun 03, 2019

Python List Comprehension

Image credit Hi there! Thought I'd give a big hi today in apologies for the lack of length that this blog post has in comparison with my somewhat hectic previous one. I can't consistently keep banging out blog posts that long though, I'm pretty busy. So, List Comprehensions Moving right along I have learnt how to use a fantastic paradigm in python for creating lists based on other lists called list comprehensions. Take the following:

A list of numbers

old_list = [1,2,3,4,5,6,7,8,9,10]

Create a new list

new_list = []

Iterate through previous list and add only the even numbers to the new list.

for number in old_list: if number % 2 == 0: new_list.append(number) Cool! So obviously what we are trying to do here is extract the even numbers from the old list. So lets try that out using list comprehensions:

A list of numbers

old_list = [1,2,3,4,5,6,7,8,9,10] new_list = [number for number in old_list if number % 2 == 0] Would you look at that? The new list creation is now a one liner! Awesome, so how does it work? Glad you asked. So, as the name suggests, python is capable of 'comprehending' lists in a simplified way that makes reading a breeze (once you get used to it). A basic format might be something like: list = [ITEM FROM ORIGINAL LIST for ITEM FROM ORIGINAL LIST in ORIGINAL LIST] Alternatively, you could swing in the opposite direction and just go to town on the conditions and chain if statements like so: new_list = [number for number in old_list if number > 1 if number < 3] result: [2] That's all for this week! Like I said, short and sweet today. Tune in next week for some more geek talk!

← Previous Next → Page 4 of 6