Ir al contenido principal

Ralsina.Me — El sitio web de Roberto Alsina

Publicaciones sobre python (publicaciones antiguas, página 88)

Truco Python: El funcionario (o la Dicción)

Su­pon­ga­mos que al­gu­na vez ex­pu­sis­te un dic­cio­na­rio co­mo par­te de la in­ter­fa­ce de un ob­je­to. Así que la gen­te vie­ne ha­cien­do co­sas co­mo es­ta:

object.data[foo]['bar']

Y ahora no querés que foo sea obligatorio, porque lo podés adivinar, o tiene un default razonable. De hecho, lo que querés exponer es esto:

object.data(bar, foo='foo')

Acá hay una idea de co­mo ha­cer­lo sin rom­per el có­di­go vie­jo:

class fundict(dict):
    def __call__(self, bar, foo='foo'):
        return self[foo][bar]

Eso es un dic­cio­na­rio que es "ca­lla­ble", con lo que es in­dis­tin­gui­ble de una fun­ció­n. Una fun­ció­n-­dic­cio­na­rio. Un fun­cio­na­rio. Y por su­pues­to lo po­dés ha­cer al re­vés e im­ple­men­tar una fun­ción que sir­va co­mo dic­cio­na­rio. Un dic­cio­na­rio­-­fun­ció­n, una dic­ció­n. Pe­ro co­mo ese es más la­bu­ro, hi­ce es­te.

Nikola 5.4 is out!

I am th­ri­lled to an­noun­ce the re­lea­se of ver­sion 5.4 of Niko­la, a sta­tic si­te/­blog ge­ne­ra­tor

He­re's the (in­com­ple­te!) chan­ge­log for this re­lea­se [1]:

Features

  • Twi­­tter Card / Open Gra­­ph su­­ppo­r­­t.

  • Smart ma­­th su­­pport

  • New soun­­dcould di­­re­c­­ti­­ve

  • Cus­­tom "read mo­­­re" li­nks

  • Be­­­tter ti­­me dis­­pla­­y, ti­­me­­zo­­­ne su­­pport

  • Be­­­tter doit in­­te­­gra­­tion (Is­­sue #151)

  • Make the who­­­le lis­­tings fo­l­­der bro­­ws­a­­ble (Is­­sue #128)

  • New GZI­­P_­­FI­­LE­S/­G­­ZI­­P_EX­­TEN­­SIONS op­­tions to crea­­te gzi­­pped co­­­pies of so­­­me fi­­les (Is­­sue #348)

  • New op­­tio­­­nal pa­­th pa­­ra­­me­­ter to new_­­post co­­­m­­man­­d.

  • Wo­r­­dpress im­­po­r­­te­­r: op­­tion to not do­­wn­­load fi­­les

  • Wo­r­­dpress im­­po­r­­te­­r: op­­tion to squash new­­li­­nes

  • Se­­pa­­ra­ted BLO­­­G_URL in­­to SI­­TE_URL and BA­SE_URL

  • Added DI­S­A­­BLE­­D_­­PLU­­GINS op­­tion (Is­­sue #354)

Bugfixes

  • Added mis­­sing </­­di­­v> in de­­fault the­­me te­m­­pla­­tes.

  • Wo­r­­dpress im­­po­r­­t: Des­­cri­p­­tion is le­­ft em­p­­ty if no des­­cri­p­­tion is foun­­d.

  • When run­­ning the build co­­­m­­mand it is now po­­s­­si­­ble to get he­l­­p.

  • Load jQue­­ry be­­­fo­­­re boots­­trap in JS bun­d­­le (Is­­sue #327)

  • Ge­­ne­­ra­­te va­­lid HT­­M­­L5 for re­­di­­re­c­­ts (Is­­sue #276)

  • Fixed "niko­­­la che­­ck"

  • Fixed in­­te­r­­na­­tio­­­na­­li­­zed RSS li­nks

  • Make co­­­m­­man­­ds that need to be run in a si­­te fail gra­­ce­­fu­­lly (Is­­sue #342)

  • Use lo­­­ca­­li­­zed li­nks on lx­­ml fixer

  • Re­­di­­re­c­­tions created du­­ring the im­­port from wo­r­­dpress are now wri­­tten to co­n­­f.­­py

  • Stop pa­r­­sing me­­ta­­da­­ta in post fi­­le on first blank li­­ne

  • Me­­ta­­da­­ta han­d­­ling clea­­nups by To­r­­dek

  • Fixed blo­­­ck­­quo­­­te font si­­ze in­­co­n­­sis­­ten­­cy

  • Wo­r­­dpress Im­­po­r­­t: Do not break in­­den­­ta­­tion (is­­sue #189)

  • Make things wo­­­rk even if SI­­TE_URL has a pa­­th (Fix #307)

Creating a Site (Not a Blog) with Nikola

One of the most fre­quent ques­tions I get about Niko­la is "but how do I crea­te a si­te tha­t's not a blo­g?". And of cour­se, tha­t's be­cau­se the do­cu­men­ta­tion is hea­vi­ly blo­g-o­rien­te­d. This do­cu­ment wi­ll chan­ge that ;-)

Sin­ce it starte­d, Niko­la has had the ca­pa­bi­li­ties to crea­te ge­ne­ric si­tes. For exam­ple, Niko­la's own si­te is a fair­ly ge­ne­ric one. Le­t's go step by step on how you can do so­me­thing like tha­t.

As usual when starting a nikola site, you start with nikola init which creates a empty semi-configured site:

$ nikola init mysite
Created empty site at mysite.

Then we go into the new mysite folder, and make the needed changes in the conf.py configuration file:

##############################################
# Configuration, please edit
##############################################


# Data about this site
BLOG_AUTHOR = "Roberto Alsina"
BLOG_TITLE = "Not a Blog"
# This is the main URL for your site. It will be used
# in a prominent link
SITE_URL = "http://notablog.ralsina.me"
BLOG_EMAIL = "ralsina@kde.org"
BLOG_DESCRIPTION = "This is a demo site (not a blog) for Nikola."

#
# Some things in the middle you don't really need to change...
#

post_pages = (
    ("pages/*.txt", "", "story.tmpl", False),
)

And now we are ready to crea­te our first pa­ge:

$ nikola new_post -p
Creating New Post
-----------------

Enter title: index
Your post's text is at:  pages/index.txt

We can now build and pre­view our si­te:

$ nikola build
Scanning posts.done!
.  render_site:output/categories/index.html
.  render_sources:output/index.txt
.  render_rss:output/rss.xml
:
:
: [Much more of the same]

$ nikola serve
Serving HTTP on 127.0.0.1 port 8000 ...

And you can see your (ve­ry emp­ty) si­te in http://­lo­calhos­t:8000

So, what's in that pages/index.txt file?

.. title: index
.. slug: index
.. date: 2013/03/01 10:26:17
.. tags:
.. link:
.. description:


Write your post here.

Title is the page title, slug is the name of the generated HTML file (in this case it would be index.html) the date doesn't matter much in not-blogs, same for tags and link. Description is useful for SEO purposes if you care for that.

And be­lo­w, the con­ten­t. By de­fault you are ex­pec­ted to use reS­truc­tu­red text but Niko­la su­ppor­ts a ton of for­ma­ts, in­clu­ding Ma­rk­do­wn, plain HT­M­L, BB­Co­de, Wiki, and Tex­ti­le.

So, le­t's gi­ve the pa­ge a ni­cer ti­tle, and so­me fake con­ten­t. Sin­ce the de­fault Niko­la the­me (ca­lled "si­te") is ba­sed on boots­trap you can use an­y­thing you like from it:

.. title: Welcome To The Fake Site
.. slug: index
.. date: 2013/03/01 10:26:17
.. tags:
.. link:
.. description: Fake Site version 1, welcome page!


.. class:: hero-unit span6

.. admonition:: This is a Fake Site

    It pretends to be about things, but is really just an example.
    So, don't click this button, it leads nowhere.

    .. class:: btn

    Click Me!


.. class:: span5

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris non nunc turpis.
Phasellus a ullamcorper leo. Sed fringilla dapibus orci eu ornare. Quisque
gravida quam a mi dignissim consequat. Morbi sed iaculis mi. Vivamus ultrices
mattis euismod. Mauris aliquet magna eget mauris volutpat a egestas leo rhoncus.
In hac habitasse platea dictumst. Ut sed mi arcu. Nullam id massa eu orci
convallis accumsan. Nunc faucibus sodales justo ac ornare. In eu congue eros.
Pellentesque iaculis risus urna. Proin est lorem, scelerisque non elementum at,
semper vel velit. Phasellus consectetur orci vel tortor tempus imperdiet. Class
aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos
himenaeos.

[And more in the same vein]

And that's it. You will want to change the SIDEBAR_LINKS option to create a reasonable "menu" for your site, you will want to hack the theme (check nikola help bootswatch_theme for a quick & dirty solution), and you may want to add a blog later on, for company news or whatever.

You can see the fi­nis­hed si­te in http://­no­ta­blo­g.­ral­si­na.­me and its fu­ll con­fi­gu­ra­tion in //­ral­si­na.­me/­lis­tings/­no­ta­blo­g/­con­f.­p­y.ht­ml

I ho­pe this was hel­pfu­l!

Load Testing Nikola

Niko­la ge­ne­ra­tes sta­tic si­tes, so load tes­ting it is not ve­ry in­te­res­tin­g, ex­cept as a ben­ch­ma­rk for the di­ffe­ren­ce be­tween sta­tic and dy­na­mic si­tes.

To­day I was told about bli­tz.io and sin­ce a qui­ck test is free, I thou­gh­t, why no­t? The re­sult is qui­te im­pres­si­ve: a cheap VP­S, using ga­tling as a web ser­ve­r, whi­ch is al­most con­fi­gu­ra­tio­n-­free and not meant for ex­tre­me­ly hi­gh load­s, can hand­le rou­gh­ly 9 mi­llion dai­ly hi­ts.

Let me say that agai­n: 9 mi­llion hi­ts. On a un­con­fi­gu­red $5 ser­ve­r. He­re's the re­port

How mu­ch tu­ning does it take for a wor­dpress ins­tan­ce to ser­ve tha­t? On what har­dwa­re?

And tha­t's on­ly half the pic­tu­re. Not on­ly are Niko­la's sta­tic pa­ges in­cre­di­bly li­ght on your ser­ve­r, we ha­ve al­so spent a lot of effort tr­ying to make in­di­vi­dual pa­ges load fas­t, for a be­tter user ex­pe­rien­ce. So­me of the te­ch­ni­ques us­ed for fas­t-­loading si­tes are:

  • Using we­­ba­sse­­ts to bun­d­­le CSS and JS in­­to si­n­­gle fi­­le­s.

  • Au­­to­­­ma­­tic su­­pport for CD­Ns for the co­­­m­­mon JS li­­bra­­ries

  • Su­­pport for tran­s­­pa­­ren­­tly using HT­­M­­L/­­CSS/­­JS/I­­ma­­ge co­m­­pres­­sion tools

So, if you try so­me­thing like YS­lo­w! on a Niko­la si­te, it wi­ll gi­ve you a sco­re of rou­gh­ly 93 up to 98 (out of 100) de­pen­ding on your web­ser­ver con­fi­gu­ra­tio­n.

BTW: I rea­lly want to get that up to 100. Rea­lly want it

So, re­mem­be­r, if your si­te can be ser­ved sta­ti­ca­ll­y, it's going to be chea­pe­r, fas­te­r, safe­r. And if you ever get 9 mi­llion hi­ts a da­y, it wi­ll be avai­la­ble.

Disclaimer:

Bli­tz.io is sen­ding me a t-s­hirt ;-)

New Nikola Tutorial

Tim van der Lin­den es­cri­bió un tu­to­rial im­pre­sio­nan­te acer­ca de có­mo usó Niko­la pa­ra crear su nue­vo si­tio shi­saa.­jp

Do­cu­men­ta to­do el pro­ce­so des­de ins­ta­lar Niko­la has­ta ha­cer su pro­pio te­ma.

Aún si ve­nís usan­do Niko­la ca­paz que ves al­go nue­vo así que va­le la pe­na leer­lo (a­par­te el te­ma que hi­zo es muy bo­ni­to :-)


Contents © 2000-2023 Roberto Alsina