ความน่าหลงไหลของ Python : Dictionaries, Tuple key

ความน่าหลงไหลของภาษา Python ที่น่าสนใจคือ Dictionaries ซึ่งเป็นการเก็บข้อมูลแบบ (key,value) ที่สามารถใช้ tuple เป็น key ได้
ตัวอย่างของ tuple เฃ่น (1,2,3) (‘ไทย’,’กรุงเทพ’,’สีลม’) อธิบายง่ายๆก็คือใน 1 key ประกอบไปด้วยคียร์ย่อย ข้างในนั่นเอง
โดยไม่ต้องแปลง key เป็น string หรือแปลงกลับจาก string เป็น key ให้ยุ่งยากวุ่นวาย

ประโยชน์ที่เห็นว่าสามารถนำมาใช้งานได้อย่างดีก็คือ เรื่องการจัดกลุ่มชุดข้อมูล
เราสามารถ ทำ group ข้อมูลโดยใช้ tuple เข้ามาช่วยเป็น key ได้อย่างสะดวก

ตัวอย่างเช่นเรา query ข้อมูลจาก Database มาเป็น row และต้องการ group ตาม column
และเข้าถึงข้อมูลตาม column ได้โดยไม่ต้อง query ข้อมูลจาก database ใหม่
ซึ่งจะดำเนินการใน memory เท่านั้น

มาดูตัวอย่างกันเลยดีกว่า

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
view raw dict.ipynb hosted with ❤ by GitHub

ERPNext new app with embeded js/css

environment structure
$home = /dev/frappe-bench/

1. Create new app

bench new-app myapp1

and key in App Description and other information.
App wil appear in

$home/apps/myapp1

App structure

myapp1
├── MANIFEST.in
├── README.md
├── license.txt
├── myapp1
│   ├── __init__.py
│   ├── config
│   │   ├── __init__.py
│   │   ├── desktop.py
│   │   └── docs.py
│   ├── hooks.py
│   ├── modules.txt
│   ├── myapp1
│   │   └── __init__.py
│   ├── patches.txt
│   ├── templates
│   │   ├── __init__.py
│   │   ├── generators
│   │   │   └── __init__.py
│   │   ├── includes
│   │   └── pages
│   │   └── __init__.py
│   └── www
├── myapp1.egg-info
│   ├── PKG-INFO
│   ├── SOURCES.txt
│   ├── dependency_links.txt
│   ├── not-zip-safe
│   ├── requires.txt
│   └── top_level.txt
├── requirements.txt
└── setup.py

2. Create webview and page controller
create html in folder www/myapp1_index.html

<p class="intro">Car</p>
<p>Brand: {{car.brand}}</p>
<p>Color: {{car.color}}</p>

create py in folder www/myapp1_index.py for page context data

import frappe

def get_context(context):
context.car = dict(brand="Toyota",color="Red")

3. Create js and css
create folder public/js and public/css in ../myapp1/myapp1/
for public/css create myapp-demo.css

.intro {
background-color:yello;
}

for public/js create myapp-demo.js

alert("Hello from myapp-demo.js");

Create build.json at public/build.json to config the js/css file

{
   "myapp1/css/myapp1.css": [
     "public/css/myapp-demo.css"
   ],
   "myapp1/js/myapp1.js": [
     "public/js/myapp-demo.js"
   ]
}

4. From hooks.py file add configuration

# include js, css files in header of web template
web_include_css = "/assets/myapp1/css/myapp1.css"
web_include_js = "/assets/myapp1/js/myapp1.js"

5. Final app tree

myapp1
├── __init__.py
├── config
│   ├── __init__.py
│   ├── desktop.py
│   └── docs.py
├── hooks.py
├── hooks.pyc
├── modules.txt
├── myapp1
│   ├── __init__.py
├── patches.txt
├── public
│   ├── build.json
│   ├── css
│   │   ├── myapp-demo.css
│   │   └── myapp1.css
│   └── js
│       ├── myapp-demo.js
│       └── myapp1.js
├── templates
│   ├── __init__.py
│   ├── generators
│   │   └── __init__.py
│   ├── includes
│   └── pages
│       └── __init__.py
└── www
    ├── myapp1_index.html
    ├── myapp1_index.py

6. Install app with command

bench --site site1.local install-app myapp1
bench --site site1.local sync-www

7. Run app

http://localhost:8000/myapp1_index

Source code
https://github.com/wisaruthk/myapp1.git

Design a site like this with WordPress.com
Get started