Nugroho's blog.

Thursday, December 22, 2011

Obat Tidur Dosis Super

Saat ini aku susah bergerak, bahkan untuk batuk pun susah karena perut terasa kram. Bukan karena terkena penyakit tertentu, melainkan karena obat tertentu.

Tiga hari ini, tenggorokan terasa panas, bukan seperti batuk pilek biasa. Setelah periksa ke bidan desa, ternyata tenggorokanku ada semacam sariawan!!! Difteri, hm, sudah hampir dua tahun tidak mengalami lagi, bukannya ingin sakit sih, cuma gak bisa lupa sakit yang dua tahun lalu, suaraku sampe berubah jadi pecah kayak robot. Kalo bisa diatur agar suaranya bisa suara prime-tertz-quin sih tidak masalah, :(



"Suntik mas?" (waduh...)
"Hehehe..."
"Ok"

Bidan ambil alat suntik dan mencobloskan jarumnya ke ampul obat putih bening, seperempat dari isi keseluruhan alat suntik. Setidaknya yang disuntikkan tidak banyak, pikirku. Eh, tapi kok obatnya aneh?

"Bu, kok obatnya bening. Kayaknya dulu warnanya merah deh"
"Memang, lha ini yang merah" bu bidan mencobloskan jarum lagi ke ampul merah. Sekarang sudah setengah alat suntik terisi cairan, deg...deg..deg.
"Pernah alergi obat Mas?"Aku cuma bisa geleng, mulutku gak bisa kupercaya untuk mengeluarkan sepatah kata tanpa bergetar.

"Kalo gitu tak tambahkan ini" Sekarang alat suntik sudah berisi PENUH cairan obat yang akan disuntikkan ke tubuhKU..., duh gusti...

"AAAOOOUUUWWW..."

"Walah, sudah bolak balik ke sini berpuluh tahun kok ya masih sama gayanya"

"AAUUWWW..."

Semenit kemudian barulah dicabut. Aku cemberut.

Beberapa menit kemudian
"Nah, yang ini diminum kalo diare, 2 buah pil, jika dan hanya jika diare lho. Kalo nggak diare nggak usah di minum. Yang putih kecil ini untuk pilek tiga kali sehari , tapi minumnya besok, soalnya di suntikkan tadi sudah ada. Nah kalo yang untuk pusing bisa disimpan untuk suatu saat kalo pusing lagi, tiga kali sehari, kalo sudah tidak pusing hentikan minumnya, yang ini antibiotik....blablabla..."

Begitulah aku diberi obat 11 macam pil yang fungsinya macam-macam, dari diare, pusing, pilek, demam, sariawan ditenggorokan sampai jamur di usus. Hm, setidaknya aku masih tahan tidak pingsan mendengarnya, kalau pil sih, kecil... Suntikan? No Way...

Malamnya langsung minum obat, tidak ada masalah

Dini hari. Terbangun, keringat dingin yang melimpah tanda sembuh, mau merubah posisi tidur, eh loh? Kok badanku kaku? Coba menengokkan kepala, tidak bisa. Kupaksa bangun dengan cara normal, YA AMPUN, perutku rasanya seperti baru sit-up tujuhratus tujuhpuluh tujuh kali, njarem (ngilu). Pasrah, tidur lagi dengan posisi yang sama, obat tidurnya masih berpengaruh kuat.

Subuh terbangun lagi, mau menggeser kaki pun rasanya seperti habis lari marathon tanpa pemanasan. Kupaksa bangun sambil teriak-teriak, rasanya seluruh tubuhku habis digebuki orang (hm, mungkin rasanya gitu, gak pernah digebuki orang), tetap kupaksa bangun, ingin pipis.

Jalanku seperti robot, tangan kanan membantu mengangkat kaki kanan, yang kiri juga sama. Dekat kamar mandi ketemu Anggun

"Mas, kenapa"
"Gak tahu, kaku semua. Apa memang obatnya berpengaruh seperti ini?"
"Harusnya enggak, mana obatnya, tak lihatnya" Anggun juga bidan
"(nggremeng) hm, ni obat pilek ada CTM-nya, antibiotiknya ok, demakolin, loh, ini kan ada CTM-nya juga? wah ni harus diminum salah satu tok, nah kalo yang ini gak papa terus yang...."

....

"Wah, ini sampeyan dapat dua jenis pil yang ada obat penenangnya, efeknya sampeyan seperti dibius, efek sampingnya bisa kayak saya dulu, leher kaku, eh kalo sampeyan sekujur tubuh kaku, hehehe..."

Duh, dan beginilah, untuk batuk saja malah lebih susah dari sebelum diobati. Mungkin maksudnya memberi efek jera agar tidak bisa batuk, tapi... Setidaknya demam dan pilek sudah hilang, walau masih terbujur kaku.

Monday, December 19, 2011

Python-based Web Page to Compute Function with User Input Flexible Function

This is improved from my python-based web based to display function. In this version, users have ability to input a function and then display it with it value for given variable to python-based web page.

The code below will get input from users (if no input, the default value is sin(x)), parsed it to function python understand, and then eval it for given variable (in this code, x=10). After computed, it's inserted to template that resembling html code. Thus, since it's displayed in html style, we could add our customization (background, css, etc)



Here's the code


#!/usr/bin/python
# -*- coding: utf-8 -*-
import BaseHTTPServer, urllib, re
import sys,parser
from math import *

class Handler(BaseHTTPServer.BaseHTTPRequestHandler):
    template = u"""<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
        "http://www.w3.org/TR/html4/strict.dtd"><html><head><title>%s</title>
        </head><body><h1>%s</h1><pre>%s</pre>Function<form action="" method="POST"
        class="editor"><div><textarea name="text">%s</textarea><input type="submit"
        value="Compute"></div></form></body></html>"""
    
    def escape_html(self, text):
        """Replace special HTML characters with HTML entities"""
        return text.replace(
                            "&", "&amp;").replace(">", "&gt;").replace("<", "&lt;")
    
    def link_repl(self, match):
        """Return HTML for link"""
        title = match.group(1)
        if title in self.server.pages:
            return u"""<a href="%s">%s</a>""" % (title, title)
        return u"""%s<a href="%s">?</a>""" % (title, title)
    
    def do_HEAD(self):
        """Send response headers"""
        self.send_response(200)
        self.send_header("content-type", "text/html;charset=utf-8")
        self.end_headers()
    
    def do_GET(self):
        """Send page text"""
        self.do_HEAD()
        page = self.escape_html(urllib.unquote(self.path.strip('/')))
        text = self.escape_html(self.server.pages.get(page, "sin(x)"))
        parsed = re.sub(r"\[\[([^]]+)\]\]", self.link_repl, text)
        #hitung fungsi
        fungsi=parser.expr(parsed).compile()
        x = 10
        y = eval(fungsi)
        tout = 'The value of  ',parsed, ' on x = ',x,' is ',y
        tout = str(tout)
        tout = re.sub(r",", "", tout)
        tout = re.sub(r"\'", "", tout)
        tout= tout[1:]
        tout= tout[:-1]
        self.wfile.write(self.template % (page, page, tout, text))
    
    def do_POST(self):
        """Save new page text and display it"""
        length = int(self.headers.getheader('content-length'))
        if length:
            text = self.rfile.read(length)
            page = self.escape_html(urllib.unquote(self.path.strip('/')))
            self.server.pages[page] = urllib.unquote_plus(text[5:])
        self.do_GET()

if __name__ == '__main__':
    server = BaseHTTPServer.HTTPServer(("", 8080), Handler)
    server.pages = {}
    server.serve_forever()

Here's the screenshot
From python

Sunday, December 18, 2011

User Input on Python during Runtime

Eventually, we want interactivity when executing Python script. We want user to give input for some variable. It'll useful for, lets call, application form where user have to input her/his name, age, etc. On math field, user will have flexibility to input the function and range of variable used to computation.



There is raw_input command and input command we can used.

raw_input command will translate all we type to string, while input command treat it as command

Here difference between the two

raw_input
x=raw_input('type anything \n')
print 'you typed ', x

Execute it
Nugrohos-MacBook-Pro:python nugroho$ python input.py 
type anything
a
you typed a
Nugrohos-MacBook-Pro:python nugroho$ python input.py
type anything
12
you typed 12
Nugrohos-MacBook-Pro:python nugroho$ python input.py
type anything
sin(x)+x**2
you typed sin(x)+x**2
Nugrohos-MacBook-Pro:python nugroho$

input
c='sin(x)+x**2'
me='Hello folks, Aravir here'
x=input('type anything \n')
print 'you typed ', x

Execute it
Nugrohos-MacBook-Pro:python nugroho$ python input.py 
type anything
a
Traceback (most recent call last):
File "input.py", line 3, in
x=input('type anything \n')
File "", line 1, in
NameError: name 'a' is not defined
Nugrohos-MacBook-Pro:python nugroho$ python input.py
type anything
12
you typed 12
Nugrohos-MacBook-Pro:python nugroho$ python input.py
type anything
sin(x)+x**2
Traceback (most recent call last):
File "input.py", line 3, in
x=input('type anything \n')
File "", line 1, in
NameError: name 'sin' is not defined
Nugrohos-MacBook-Pro:python nugroho$ python input.py
type anything
c
you typed sin(x)+x**2
Nugrohos-MacBook-Pro:python nugroho$ python input.py
type anything
me
you typed Hello folks, Aravir here
Nugrohos-MacBook-Pro:python nugroho$

Displaying Calculation Output of Python on Web (customizing)

After success displaying output using python based web, it's normal if we want to display the value of function with a range of variable.

The code below will create web page hosted by Python 2.7 BaseHttpServer module. The page contains list of value of function sin(x)+x**2 at -7<x<7



from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
import sys,parser
from math import *
import numpy as np

class Handler(BaseHTTPRequestHandler):
def do_GET(self):
n=10
x1=-7
x2=7
y = 'sin(x)+x**2'
z = parser.expr(y).compile()

self.send_response(200)
self.send_header("Content-type", "text/html")
self.end_headers()

self.wfile.write("Hi Folks, Aravir here")
self.wfile.write("")
self.wfile.write("Hi Folks, Aravir here
")
self.wfile.write("
")
for i in (range(x1,x2)):
x=i
self.wfile.write("The value of ")
self.wfile.write(y )
self.wfile.write(" on x = " )
self.wfile.write(x)
self.wfile.write(" is " )
self.wfile.write(eval(z) )
self.wfile.write("
")

self.wfile.write("")

if __name__=="__main__":
try:
server = HTTPServer(("", 8080), Handler)
server.serve_forever()
except KeyboardInterrupt:
server.socket.close()


Here the result in Safari, Mac OS X Lion

From python

Displaying Calculation Output of Python Script to Web Page using Python-based Web Server

It'll be convenient if we could displaying output from our Python code to web page.

To convert calculation output of Python script to web page we need BaseHTTPServer, a Python-based web server. With it, we could write any python code and display it in no time. It has advantage in form of simplicity, we don't need php to convert our result or typing it to static html code, we just used python alone (it's possible to write html and php code on python though).
This code below will display python script calculating value of a function (sin(x)+x^2) to web page. As it behave as web server too, we don't need apache or other web server to broadcast it.

Here the code. It's written in Python 2.7 on Mac OS X Lion with numpy module and sys, parser and basehttpserver built in module.

from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
import sys,parser
from math import *
import numpy as np

class Handler(BaseHTTPRequestHandler):
def do_GET(self):
n=10
x=7
y = 'sin(x)+x**2'
z = parser.expr(y).compile()

self.send_response(200)
self.send_header("Content-type", "text/html")
self.end_headers()

self.wfile.write("Hi Folks, Aravir here")
self.wfile.write("")
self.wfile.write("Hi Folks, Aravir here
")
self.wfile.write("
")
self.wfile.write("The value of ")
self.wfile.write(y )
self.wfile.write(" on x = " )
self.wfile.write(x)
self.wfile.write(" is " )
self.wfile.write(eval(z) )

self.wfile.write("")

if __name__=="__main__":
try:
server = HTTPServer(("", 8080), Handler)
server.serve_forever()
except KeyboardInterrupt:
server.socket.close()

Access it using web browser in localhost:8080 from your computer running code above, or :8080 and get this


From python

Creating Web Page using Python including its Webserver (basehttpserver module)

It's possible to create web page using Python, even self hosted it using Python built in webserver (basehttpserver module)

Here the code. It'll create web server with port 8080 and if it's accessed, it'll show a page. In the code below, page showed for us is just plain text "magic content goes here". I am planning to investigate this self.wfile.write behavior.




from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer

class Handler(BaseHTTPRequestHandler):
def do_GET(self):
self.send_response(200)
self.send_header("Content-type", "text/html")
self.end_headers()

self.wfile.write("magic content goes here")

if __name__=="__main__":
try:
server = HTTPServer(("", 8080), Handler)
server.serve_forever()
except KeyboardInterrupt:
server.socket.close()

Access with browser in address localhost:8080 or <whatever your ip address>:8080 and get this

From python

Django ain't for This

While I'm searching for tutorial about creating simple static web page using Django, I surprised that i found almost nothing. There is Django documentation for it but I think it's too much; creating microblogger, forum, note, wiki, etc.

According what I found after surfed a while, Django isn't for this. I was suggestet to use basehttpserver instead.



Here what I found

"I have python scripts which provides output and I need to have this output on the web."


That is not what Django is for. What you want to do can be achieved with something as simple as this:

from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer

class Handler(BaseHTTPRequestHandler):
def do_GET(self):
self.send_response(200)
self.send_header("Content-type", "text/html")
self.end_headers()

self.wfile.write("magic content goes here")

if __name__=="__main__":
try:
server = HTTPServer(("", 8080), Handler)
server.serve_forever()
except KeyboardInterrupt:
server.socket.close()
Observe the self.wfile.write line. Whatever you write there ends up in the browser. If it matters, you can use self.path in the Handler to check which file was requested.

Tested with Python 2.6.4, accessed the server with Chrome browser.

..........

If you need a quick web server running and you don't want to mess with setting up apache or something similar, then Python can help. Python comes with a simple builtin HTTP server. With the help of this little HTTP server you can turn any directory in your system into your web server directory. The only thing you need to have installed is Python.

Practically speaking this is very useful to share files inside your local network. Implementing this tiny but hugely useful HTTP server is very simple, its just a single line command.

Assume that I would like to share my home directory
Open up a terminal and type:

$ cd 
$ python -m SimpleHTTPServer

That's it! Now your http server will start in port 8000. You will get the message:
Serving HTTP on 0.0.0.0 port 8000 ...

Now open a browser and type the computer address like:
http://192.168.1.1:8000

If you on computer that is running server, you can also access it via:
http://127.0.0.1:8000

If the directory has a file named index.html, that file will be served as the initial file. If there is no index.html, then the files in the directory will be listed.
If you wish to change the port that's used start the program via:

$ python -m SimpleHTTPServer 8080

If you want to only serve on localhost you'll need to write a custom Python program such as:


import sys
import BaseHTTPServer
from SimpleHTTPServer import SimpleHTTPRequestHandler

HandlerClass = SimpleHTTPRequestHandler
ServerClass = BaseHTTPServer.HTTPServer
Protocol = "HTTP/1.0"

if sys.argv[1:]:
port = int(sys.argv[1])
else:
port = 8000
server_address = ('127.0.0.1', port)

HandlerClass.protocol_version = Protocol
httpd = ServerClass(server_address, HandlerClass)

sa = httpd.socket.getsockname()
print "Serving HTTP on", sa[0], "port", sa[1], "..."
httpd.serve_forever()


323f (5) amp (1) android (12) apple (7) arduino (18) art (1) assembler (21) astina (4) ATTiny (23) blackberry (4) camera (3) canon (2) cerita (2) computer (106) crazyness (11) debian (1) delphi (39) diary (286) flash (8) fortran (6) freebsd (6) google apps script (8) guitar (2) HTML5 (10) IFTTT (7) Instagram (7) internet (12) iOS (5) iPad (6) iPhone (5) java (1) javascript (1) keynote (2) LaTeX (6) lazarus (1) linux (29) lion (15) mac (28) macbook air (8) macbook pro (3) macOS (1) Math (3) mathematica (1) maverick (6) mazda (4) microcontroler (35) mountain lion (2) music (37) netbook (1) nugnux (6) os x (36) php (1) Physicist (29) Picture (3) programming (189) Python (109) S2 (13) software (7) Soliloquy (125) Ubuntu (5) unix (4) Video (8) wayang (3) yosemite (3)