Skip to main content

Ralsina.Me — Roberto Alsina's website

Posts about python (old posts, page 96)

Brute Force Works

Last night, Juan­jo Con­ti tweet­ed this:

Or, in en­glish: "Us­ing ex­act­ly once the dig­its 1,3,4 and 6, and any of the four ba­sic op­er­a­tions, ob­tain 24."

I first spent a cou­ple of min­utes think­ing about it and then it hit me: there is no point in think­ing this sort of prob­lem, be­cause:

  1. Brute for­c­ing it will take less time

  2. What you do while "think­ing" it is sort of lame, is­n't it?

So, here is a more-or-­less gen­er­al so­lu­tion for any of these prob­lem­s.

from __future__ import print_function, division
import itertools

numbers = ['1','3','4','6']
target = 24

# Having '' as an operation allows for solution (14-6)*3, which
# may or may not be valid depending on rule interpretation.
operations =  ['*','/','+','-','']


for nums in itertools.permutations(numbers):
    for ops1 in itertools.combinations_with_replacement(operations, 3):
        for ops2 in itertools.permutations(ops1):
            for t in (t1, t2):
                s = t.format(*(nums+ops2))
                    if eval(s) == target:
                except (ZeroDivisionError, SyntaxError, TypeError):

Of course you can make it solve any problem of this class by adjusting numbers and target. There is also a possible extra solution if eval(s) == -tar­get where you just need to add a unary - to the expression, but who cares.

Did I miss some­thing? Is this re­al­ly a gen­er­al so­lu­tion?

FLOSS Decision Making in Action

If you are read­ing this there is a good chance you are in­volved some­how in open source de­vel­op­men­t, or soft­ware de­vel­op­ment in gen­er­al. One thing lots of peo­ple ask me when they know I have lead this sort of projects for a long time is "how do you de­cide things?". To which I have all sorts of bad an­swers like:

  • "It's a con­sen­­sus thing"

  • "It hap­pens or­­gan­i­­cal­­ly"

  • "Some­­times it just hap­pen­s"

  • "A­n­ar­chy!"

  • "Y­ou do what you do"

So, now here I have an AWE­SOME ex­am­ple of FLOSS de­ci­sion mak­ing in ac­tion, which is ... all of the above.

Some con­tex­t: Niko­la is a stat­ic site gen­er­a­tor, so it deals with read­ing and writ­ing tex­tu­al da­ta from disk. It's al­so an in­ter­na­tion­al­ized pro­jec­t, which sup­ports mul­ti­lin­gual sites and trans­lat­ed da­ta. It al­so runs un mul­ti­ple plat­form­s, like Win­dows, OS­X, Lin­ux, etc.

And to make that more fun, it also works on Python 2.7, and 3.3 or later. Which means it has to handle two different models on how to work with unicode data, in the same codebase. And that's not fun. So, we have been floating around the idea of deprecating python 2.7. And so, when s2hc_johan walks in with a unicode problem...

14:23:16 <s2hc_johan> I don't have a site with sections, but I tested it for the other case
14:35:42 <s2hc_johan> strange it worked for a while broken again, probably because I've got åäö in it now.
14:35:45 <s2hc_johan>
14:36:17 <s2hc_johan> if you wrap data with unicode it works, but I'm not sure that works in python3
14:36:37 <ChrisWarrick> s2hc_johan: how do you wrap it with unicode?
14:36:48 <s2hc_johan> unicode(data)
14:37:05 <s2hc_johan> but is that valid in  python3?
14:37:11 <ChrisWarrick> s2hc_johan: this is wrong on so many levels
14:37:16 <ChrisWarrick> s2hc_johan: please don’t do that, ever
14:37:48 <ChrisWarrick> s2hc_johan: This won’t work in Python 3 either.  You must have an actual encoding, and use the decode method.   try: foo = foo.decode('utf-8'); except AttributeError: foo = foo  # python 3
14:38:02 <s2hc_johan> what do you mean, that is like my standard when I get strnage data in, undoce(data) data.encode(whatever) data.decode(whatever) :)
14:38:23 <s2hc_johan> one of them ussually work
14:39:22 <ChrisWarrick> s2hc_johan: unicode() assumes ASCII, it never works right
14:39:32 <s2hc_johan> true
14:39:40 <ChrisWarrick> s2hc_johan: encode/decode with a specified encoding is fine
14:40:00 <ChrisWarrick> s2hc_johan: but you might need a try/except for Python 3 if it could have Unicode data already
14:40:16 <s2hc_johan> I'm a bit confused in this case since the output comes from json.dumps
14:40:34 <s2hc_johan> thought that would produce a unicode object
14:40:51 <ChrisWarrick> s2hc_johan: not necessarily on python 2
14:41:05 <ralsina_> if isinstance(thing, utils.str_bytes): thing=thing.decode('utf8')
14:41:15 <ralsina_> that works in py2 and py3
14:42:12 <ChrisWarrick> easier to ask for forgiveness imo
14:43:07 <ralsina_> maybe we should have helpers in utils enforce_unicode and enforce_bytes
14:43:13 -GitHub[nikola]:#nikola- [nikola] Aeyoun pushed 1 new commit to feed-previewimage:
14:43:13 -GitHub[nikola]:#nikola- nikola/feed-previewimage 4b79e20 Daniel Aleksandersen: Deprecated RSS_READ_MORE_LINK and RSS_LINKS_APPEND_QUERY...
14:44:58 <Aeyoun> Or upgrade to Py3.
14:45:11 <ChrisWarrick> ++
14:45:47 <Aeyoun> Unicode in Py27 is a nightmare. It tries as hard as it can to kill you at every turn.
14:48:09 -travis-ci:#nikola- getnikola/nikola#6426 (feed-previewimage - 4b79e20 : Daniel Aleksandersen): The build is still failing.
14:48:10 -travis-ci:#nikola- Change view:
14:48:10 -travis-ci:#nikola- Build details:
14:48:27 <ralsina_> ok, let's consider py3-only seriously.
14:48:40 <ralsina_> 1) Is there any distro commonly used with py3 < 3.3 ?
14:48:55 <ralsina_> 2) Do we just stop using py2, or we deprecate slowly?
14:49:15 <ralsina_> 3) Do we just start doing py3-only code, or we actively de-hack the codebase?
14:49:21 <ralsina_> That's my 3 questions :-)
14:50:13 <SteveDrees> Unicode is a nightmare
14:50:53 <SteveDrees> different python versions just changes where the pain point is
14:50:53 <s2hc_johan> which one is better isinstance... or hasattr('decode', ..)
14:51:02 <ralsina_> isinstance
14:51:08 <s2hc_johan> oki then
14:51:10 <ralsina_> hasattr is evil in itself
14:51:26 <s2hc_johan> just going to feed the kids then I'll make another pr
14:51:28 -GitHub[nikola]:#nikola- [nikola] Aeyoun pushed 1 new commit to feed-previewimage:
14:51:28 -GitHub[nikola]:#nikola- nikola/feed-previewimage 4c950ac Daniel Aleksandersen: flake8
14:52:13 <Aeyoun> ralsina_: user survey? pip download data?
14:52:33 <gour> ralsina_: create some poll at website/mailing-list about it?
14:53:18 <ralsina_> dude, I offered free shirts and I got only 10 requests ;-)
14:53:30 <ralsina_> so, how many answers do you expect about that sort of thing?
14:53:43 * gour thought shirts are jsut for devs :-(
14:53:47 <Aeyoun> ralsina_: release a unchanged version on pip that is flagged as py3 only. see how many downlaod it versus previous version in same amount of time.
14:53:51 <ralsina_> gour: go add yourself dude
14:54:18 <ralsina_> gour: TO THE SHIRT LIST! I just notced that sounded very rude :-)
14:54:43 <gour> ralsina_: where it is?
14:54:43 <Aeyoun> ralsina_: or one py27 version number and and one version py3 only version number at the same time.
14:55:17 <ralsina_> gour:
14:55:17 <gour> found it
14:56:00 <gour> ralsina_: wonder if xxl is too large or xl is enough
14:56:00 <Aeyoun> ralsina_: american or european sizes by the by?
14:56:03 <ralsina_> Aeyoun: that reflects how many people use py2.7 by reflex. I know *i* do because it's "python" and not "python3"
14:56:20 <ralsina_> Aeyoun: no idea about sizes to be honest... probably american
14:56:21 <Aeyoun> American sizes are … a big bigger. I’m probably a XS/S american but M european. :P
14:56:28 <Aeyoun> *bit bigger
14:56:39 <gour> ok
14:56:57 * gour submitted request
14:57:17 <ralsina_> So, what I would prefer to do is make people use py3 if they can. And it seems to me that pretty much everyone can, regardless of whether they still use py2 by defect.
14:57:26 <ralsina_> by default*, spanishism leaked there.
14:57:52 <ChrisWarrick> technically, using py2 is a defect
14:57:59 <ralsina_> So, if we all agree that most users *could* run nikola in py3... then let's do it.
14:58:02 <Aeyoun> Agreed.
14:58:15 <gour> sites won't stop working :-)
14:58:26 <Aeyoun> ralsina_: act on data not dev agreement?
14:58:42 <ChrisWarrick> guess we could change our docs/webiste to highlight 3.x
14:58:59 <ralsina_> Aeyoun: the only data we'd need is to know how many people have py2.7 and no py3.3
14:59:14 <ralsina_> not how many are *using* 2.7 instead of 3.3
14:59:38 <ChrisWarrick> micro-survey via ml?
14:59:39 <ralsina_> How about: let's announce that, unless lots of people complaint, we deprecate py2 by end of october
14:59:45 -travis-ci:#nikola- getnikola/nikola#6429 (feed-previewimage - 4c950ac : Daniel Aleksandersen): The build was fixed.
14:59:46 -travis-ci:#nikola- Change view:
14:59:46 -travis-ci:#nikola- Build details:
14:59:47 <Aeyoun> Mac is shipping with Py2.7 and no Py3. BUT MacPorts and Homebrew offer painfree Py3 installs.
14:59:58 <ralsina_> ok, mac is a good point
15:00:25 <ChrisWarrick> it’s not like we have Homebrew/MacPorts/Fink-based install instructions for them…
15:00:27 <Aeyoun> ralsina_: we could add a deprecation message every time `nikola` is run and ask people to bitch in a bug?
15:00:32 <Aeyoun> ChrisWarrick: hehe. ;)
15:00:50 <ralsina_> "I see you have python3 installed but I am running on 2.7 ... dude, what's wrong with you?"
15:00:51 <Aeyoun> Or maybe once per 24 hour rather  than every time its run.
15:01:00 <ralsina_> doit timed tasks :-)
15:01:12 <Aeyoun> ralsina_: "Don’t get in the way of progress! Upgrade to Py3 and save a developer’s mind today!"
15:01:32 <ralsina_> "niec unicode you have there, would be a shame something happened to it.. switch to python 3!"
15:01:39 <ChrisWarrick> ralsina_: hey, let’s start with a Google Docs survey on the ML.  One question: what Python version and OS are you using for Nikola? 2.7/3.3/3.4/3.5; Windows/OS X/[other: linux/bsd distro]
15:01:57 <gour> "Free t-shirt foreveryone switching from py2.7 to py3.3"
15:01:58 <ChrisWarrick> ralsina_: Just don’t require a Google account like you did last time.
15:02:00 <ralsina_> Second question: "Do you have python 3.3 or later installed?"
15:02:03 <Aeyoun> How much code can be removed with dropping Py27? Lowers maintenance cost and increases performance. That is also an important datapoint.
15:02:11 <ralsina_> ChrisWarrick: I needed to know who was asking for the shirt :-)
15:02:21 <ChrisWarrick> ralsina_: good point
15:02:25 <ralsina_> Aeyoun: not all that much, really
15:02:47 <ChrisWarrick> Aeyoun: it would need to start with a huge rewrite to remove all of our pointers in nikola.utils
15:03:00 <ralsina_> Aeyoun: there are a number of tiny hacks, which were a pain to get right but they always amount to one if and/or one decode :-)
15:03:26 <ralsina_> We can just turn a bunch of helpers in utils into noops
15:04:52 <gour> py3-only nikola is going to become v8?
15:05:15 <Aeyoun> gour: seems like a likely outcome. you’re following the discussion live.
15:06:34 <ChrisWarrick> if we do v8, we’ll have to merge the early tasks garbage
15:07:03 <ralsina_> Is it technically backwards-incompatible if we just stop working on py2.7?
15:07:21 <ralsina_> gour: welcome to open source software: behind the code.
15:07:30 <gour> ralsina_: :-)
15:07:35 <Aeyoun> Someone call in a documentary crew!
15:07:43 <ralsina_> Aeyoun: we have logs!
15:07:51 <Aeyoun> Oh, wait. This is already logged for prosperity.
15:07:57 <ralsina_> I am totally posting this somewhere as "this is how decisions are made in FLOSS"
15:08:40 <ralsina_> Ok, who creates the poll and who posts it in the blog, and who makes sure it appears on planet, and who sends it to the list?
15:08:49 <ralsina_> I would do it but I have work to do :)
15:08:51 <ChrisWarrick> ralsina_: I’ll do it
15:08:57 <ralsina_> ChrisWarrick: you rock dude!
15:09:01 <ChrisWarrick> ralsina_: should be really simple
15:09:03 <ralsina_> Ok, we have a plan!
15:09:17 <ralsina_> Let's consider the poll results in ... a week?
15:09:25 <Aeyoun> Let the logs show we’re all in favor of this plan of action. ;-)
15:09:29 <ralsina_> aye
15:09:51 <ralsina_> Also: can I do the "shame on you" thing on nikola build? It sounds like fun :-)
15:10:27 <ChrisWarrick> ralsina_: for the python version question: radiobox vs checkbox?
15:10:28 <gour> ralsina_: you can mention that Nikola (Tesla) was always for innovation ;)
15:10:44 <Aeyoun> "You’re using FIVE YEAR OLD SOFTWARE. Update your system."
15:11:00 <ralsina_> Aeyoun: I am totally getting at least 5 different comments there
15:11:01 <Aeyoun>
15:11:05 <ralsina_> ChrisWarrick: checkbox... maybe 2?
15:11:23 <ralsina_> ChrisWarrick: one for python version, one for operating system
15:11:32 <ChrisWarrick> ralsina_: ?
15:11:38 <ralsina_> ChrisWarrick: two questions
15:11:54 <ChrisWarrick> ralsina_: there will even be three questions (py2/3 used, OS, has py3)
15:11:57 <ChrisWarrick> ralsina_: and checkboxes it is
15:12:02 <ralsina_> right
15:12:05 <ralsina_> awesome
15:14:44 <ralsina_> Copied / Pasted for posterity

There you go, half an hour lat­er, we have a plan to (may­be) dep­re­cate it.

Now go vote here: Should Niko­la sup­port python2.7? Gives us da­ta to de­cide!

Nikola v7 finally out!

I am thrilled to an­nounce ver­sion 7 of Niko­la, a stat­ic site and blog gen­er­a­tor is out, with a bazil­lion fea­tures and bug­fix­es (see be­low).

You can get it at all the usu­al places, and here's the re­lease an­nounce­ment

Here's the new fea­tures, the bug­fix­es list would make the post too long :-)

  • Added UN­SLUGI­FY_TI­TLES op­tion for mak­ing ti­tles fetched via the file­name reg­exp pret­ti­er (Is­sue #1282)

  • New de­pen­den­cies: nat­sort (nat­u­ral sort­ing in gal­leries) and da­teu­til (re­places pytz)

  • Niko­la.­­com­­mands are now the user-friend­­ly wrap­pers from con­­sole (Is­­sue #1177)

  • Add a github_de­ploy com­mand to de­ploy to GitHub pages (Is­sue #1208)

  • Re­­move tidy fil­ter (it was bro­ken due to tidy be­ing an­­cien­t) (Is­­sue #1164)

  • Added GEN­ER­ATE_RSS set­ting to al­low dis­abling RSS in Niko­la (Is­sue #1236)

  • Link list­ings raw sources if COPY­­_­­SOURCES is True (Is­­sue #1214)

  • Much more pow­er­ful niko­la plug­in com­mand (Is­sue #1189)

  • More pow­er­­ful con­­sole mode al­lows ac­cess to all niko­la com­­mands (Is­­sue #830)

  • New `ROBOT­S_EX­CLU­SION­S` op­tion list­ing re­sources to ex­clude from sitemap and in­clude in new gen­er­at­ed /robot­s.txt (Is­sue #804)

  • Gen­er­ate sitemap­in­dex con­­tain­ing RSS and sitemap files (Is­­sue #804)

  • Sup­­port hooks in tem­­plates, for use by plug­ins (Is­­sue #896)

  • Use read­­­line if avail­able (Is­­sue #1238)

  • Re­placed READ­_­MORE_LINK with IN­DEX_READ­_­MORE_LINK and RSS_READ­_­MORE_LINK (Is­sue #1222)

  • Added read­­ing_­­time, re­­main­ing_read­­ing_­­time, para­­graph_­­coun­t, re­­main­ing_­­para­­graph_­­count tags for READ­­_­­MORE_LINK (Is­­sue #1220)

  • Add canon­i­­cal link in list­ings.

  • Added sup­­port for new meta files that are the same for­­mat as 1-­­file meta­­data, al­low­ing for greater flex­i­­bil­i­­ty (Is­­sue #954)

  • Col­or­box is now in­­ter­­na­­tion­al­ized (Is­­sue #1205)

  • Added LO­­GO_URL and SHOW_BLOG_TI­TLE=True set­t­ings to fa­­cil­i­­tate show­ing off lo­­gos (Is­­sue #1122)

  • Cre­ate au­­to­­mat­ic sto­ry in­­dex pages for sub­­­fold­er­s, too (Is­­sue #793)

  • New Slo­­vak tran­s­la­­tion by Tomáš Prékop

  • Cre­at­ed a Mark­­downEx­ten­­sion plug­in class (Is­­sue #1175)

  • The base theme pro­­duces prop­er­­ly sec­­tioned and se­­man­tic HT­M­L5 (Is­­sues #1123, #1137)

  • The base theme comes with a new stylish look by de­­fault (Is­­sue #1137)

  • The base theme sup­­ports Right-­­to-Left by us­ing ::dir(rtl) CSS4 rules and <html dir="rtl"> tags where valid (Is­­sue #1146)

  • Boot­s­trap 2 up­­­dat­ed to 2.3.2 (via Is­­sue #1137)

  • Added FORCE_ISO8601 set­t­ing that cur­ren­t­­ly makes new_­­post use ISO 8601 dates (via Is­­sue #1156)

  • Added sup­­port for TZ spec­i­­fied in post date (Is­­sue #1118)

  • Make niko­la init ask about the site’s set­tings (Is­sue #1080)

  • Use nat­u­ral sort­ing for files and fold­ers list in list­ings and gal­­leries (Is­­sue #1144)

  • Added in­­­var­i­ance test­ing (Is­­sue #672)

  • Plug­ins can in­­­ject tem­­plates in the sys­tem (Is­­sue #1139)

  • niko­la im­port_­word­press now has a --q­­tran­s­late op­tion, to parse posts in the qtrans­late word­press plug­in for­mat and turn them in­to mul­ti­lin­gual Niko­la posts (Is­sue #1072)

  • niko­la con­sole al­lows for in­ter­preter choice via -b, -i, -p; more­over, sup­port for bpython is not dep­re­cat­ed any­more (Is­sue #1126)

  • re­tired tag for posts has been re­placed with pri­vate (via Is­sue #686)

  • Changed the de­­fault TRAN­S­LA­­TION­S_­­PAT­TERN to "{­­path}.{lang}.{ex­t}". (Is­­sues #990, #829)

  • Back­­wards com­­pat­i­­bil­i­­ty with v5 is bro­ken. Added back­­ward­s-in­­com­­pat­i­ble changes. (Is­­sue #829)

  • Added a CON­TENT_­FOOTER_­FOR­MATS con­fig op­tion. It is used to for­mat the CON­TENT_­FOOT­ER vari­able prop­er­ly, for com­pat­i­bil­i­ty with the Trans­lat­able Set­tings fea­ture. The vari­able takes a dic­t, the keys of which are lan­guages, and val­ues are (args, kwargs). (Is­sue #1112)

  • Cer­­tain set­t­ings are now tran­s­lat­able. As of now, the set­t­ings are: BLOG_AU­THOR, BLOG_TI­TLE, BLOG_DE­SCRIP­­TION, LI­­CENSE, CON­­TEN­T_­­FOOTER, SO­­CIAL_BUT­­TON­S_­­CODE, SEARCH_­­FOR­M, BODY_END, EX­­TRA_­­HEAD­­_­­DATA, NAV­I­­GA­­TION_LINKS, READ­­_­­MORE_LINK (the up­­-­­to-­­date list is avail­able in SITE.­­TRAN­S­LAT­ABLE_SET­T­INGS) (Is­­sues #851, #1057, #1061, #1112)

  • New­thor() re­­turns meta 'au­thor' or BLOG_AU­THOR (Is­­sue #1117)

  • Ship base-jin­­ja, boot­s­trap-jin­­ja, boot­s­trap3-jin­­ja with Niko­la (Is­­sue #1104)


  • Re­­move old mes­sages left over for back­­wards com­­pat­i­­bil­i­­ty: (Is­­sues #829, #1105)

    • "More posts about", re­­­placed by "More posts about %s"

    • "Post­ed", re­­­placed by "Post­ed:"

    • "Al­­­so avail­able in­­­", re­­­placed by "Al­­­so avail­able in­­­:"

  • Re­­move old "s­l_SI", "tr_TR" lo­­cale alias­es (use "s­l" and "tr") (Is­­sue #829, #1105)

  • New op­­tion RSS_­­PLAIN to op­­tion­al­­ly strip HTML from RSS feeds (Is­­sue #1107)

  • Sup­­port con­­tent key in com­pil­er­s' cre­ate_­­post (Is­­sue #1098)

  • Use se­tup­tool­s’ ex­tras fea­ture. Use pip in­stall niko­la[ex­tras] to in­stall Niko­la with ex­tras (re­quire­­ments-ex­­tras.txt, for­mer­ly re­quire­­ments-­­ful­l.txt -- note the name change!) (Is­sue #1089)

Nikola Logo Contest!

So you have de­sign skill­s? Want to make a lit­tle mon­ey while do­ing a good thing? Then join the con­test! there is a 200 US dol­lars guar­an­teed prize, and the con­test will be judged by a group of Niko­la de­vel­op­er­s.

Some guid­ance:

  • The name Niko­la comes from Niko­la Tes­la, so think con­­cepts con­nec­t­ed to that

    • The War­­­den­­­clyffe tow­er

    • Tes­la Coils

    • Light­n­ing

    • Elec­tric­i­­­ty

    • Ear­­­ly 20th cen­­­tu­ry

  • I hope the lo­­go works well in a va­ri­e­ty of for­­mats so, keep it sim­­ple. B/W is good.

  • A draw­ing with­­out the word Niko­la is good, but a good de­sign with the word in it could be good al­­so.

The con­test lasts a week, you have 4 days for the qual­i­fy­ing round.

And re­mem­ber: it has to be bet­ter than these

Nikola is now more compatible with Sphinx

Some­times, you need to use the same text in more than one place. For ex­am­ple, you may want to use frag­ments of your soft­ware man­u­als in your soft­ware's site.

If you are part of the Python-­verse then it's like­ly that your docs are writ­ten us­ing Sphinx. Ǹiko­la and Sphinx share roots in re­Struc­tured­Text so they are al­ready com­pat­i­ble in many im­por­tant ways.

How­ev­er, Sphinx adds a whole bunch of ex­ten­sions to re­Struc­tured­Text and Sphinx users use them. So, what's a dev to do? He'll write code. So I wrote a niko­la plug­in which adds sup­port for a large chunk of the Sphinx-spe­cif­ic markup.

How big a chunk? Quite big since it adds sup­port for over 35 ex­ten­sion­s, roles and di­rec­tives (no, toc­tree is not one of them yet).

This way, you may be able to en­joy a good doc­u­men­ta­tion-spe­cif­ic tool and a (if I may say so my­self) good web­site tool from com­mon sources.


Contents © 2000-2024 Roberto Alsina