heuer / segno

Python QR Code and Micro QR Code encoder
https://pypi.org/project/segno/
BSD 3-Clause "New" or "Revised" License
606 stars 52 forks source link

Don't include blank vcard/mecard fields #125

Closed missionfloyd closed 11 months ago

missionfloyd commented 1 year ago

Can result in a less dense (and thus easier to scan) code.

Before: before

BEGIN:VCARD
VERSION:3.0
N:Doe;John;
FN:
EMAIL:john@example.com
TEL:
TEL;TYPE=FAX:
TEL;TYPE=CELL:
URL:
TITLE:
END:VCARD

After: after

BEGIN:VCARD
VERSION:3.0
N:Doe;John;
FN:
EMAIL:john@example.com
END:VCARD
heuer commented 11 months ago

Please provide more information how you created the QR codes.

missionfloyd commented 11 months ago

Those were made with my QR code extension for stable-diffusion-webui. https://github.com/missionfloyd/webui-qrcode-generator/blob/master/scripts/qrcode.py#L23

It works right if I exclude the empty fields or set them to None. This generates the "After" image above.

import segno
from segno import helpers

data = helpers.make_vcard_data(displayname="", name="Doe;John;", email="john@example.com")
qrcode = segno.make(data, micro=False, error="H", boost_error=False)
qrcode.save('vcard.png', scale=4)

But not if I pass it an empty string.

import segno
from segno import helpers

data = helpers.make_vcard_data(displayname="", name="Doe;John;", email="john@example.com", phone="")
qrcode = segno.make(data, micro=False, error="H", boost_error=False)
qrcode.save('vcard.png', scale=4)

Because it's explicitly checking for None, instead of falsy values. https://github.com/heuer/segno/blob/184c7ec15734aff48f553fdd1d332fe1aaddbbb5/segno/helpers.py#L150-L155

Of course, I could work around it with a bunch of or None's.

            data = helpers.make_vcard_data(name, displayname=args["vcard_displayname"], nickname=args["vcard_nickname"], street=args["vcard_address"], city=args["vcard_city"], region=args["vcard_state"], zipcode=args["vcard_zipcode"], country=args["vcard_country"], birthday=args["vcard_birthday"], email=args["vcard_email"] or None, phone=args["vcard_phone"] or None, fax=args["vcard_fax"] or None, memo=args["vcard_memo"], org=args["vcard_organization"], title=args["vcard_title"] or None, cellphone=(args["vcard_phone_mobile"] or None), url=(args["vcard_url"] or None))
heuer commented 11 months ago

Ok, thank you. Much better solution (testing for falsy value). I'll add a test case and merge it asap

heuer commented 11 months ago

Thanks!