Showing posts with label python. Show all posts
Showing posts with label python. Show all posts

Wednesday, January 3, 2018

csv update app with spyre python

now I created csv update app with spyre. full code is here.

Preparation

set python vision and install dataspyre.
% python -V
Python 3.5.0
 
% pip install dataspyre

set directory tree as below. "test_data.csv" is the csv file to edit. number of columns have to be 5.

% tree
.
├── csv_update.py
├── data
│   ├── test_data.csv


% cat data/test_data.csv
data1,data2,data3,data4,data5
haha,hihi,huhu,hehe,hoho
gaga,gigi,gugu,gege,gogo
rara,riri,ruru,rere,roro

Execution


% python csv_update.py

Then, access to "http://127.0.0.1:9093"
input file name and push "get data" button.



input data and push "add data" button.




push "get data" button again to check added data.
backup data added under "data" directory in case.




% tree
.
├── csv_update.py
├── data
│   ├── test_data.csv
│   ├── test_data.csv_20180103173523
│   └── test_data.csv_20180103174330

% cat data/test_data.csv
data1,data2,data3,data4,data5
haha,hihi,huhu,hehe,hoho
gaga,gigi,gugu,gege,gogo
rara,riri,ruru,rere,roro
data1,data2,data3,data4,data5

Sunday, May 28, 2017

play with python turtle

drawing by python is fun! You can start drawing with python script by importing "turtle module".

1. sample code

# coding: utf-8

import turtle
t = turtle.Pen()
while True:
    color_list = 'pink', 'green', 'purple'
    import random
    scale = random.randint(50, 100)
    print("scale %s" % scale)
    t.left(60)
    for c in color_list:
        i = 1
        print("color %s" % c)
        while i <= 360 / 60:
            print("count %s" % i)
            t.begin_fill()
            t.forward(scale)
            t.fillcolor(c)
            t.left(30)
            t.circle(scale/4, 180)
            t.right(180)
            t.circle(scale/4, 180)
            t.left(30)
            t.forward(scale)
            t.left(180)
            i = i + 1
            t.end_fill()
        scale = scale * 0.8
    t.up()
    location = random.randint(100, 300)
    print("location %s" % location)
    t.left(scale)
    t.forward(location)
    t.left(scale)
    t.forward(location)
    t.down()
turtle.done()

2. execution

% python turtle_work.py

hit and blow game coded by python

coding "hit and blow game" is a best way to get start studying new program language. I coded by python with my friends.

1. rules

- find out correct 3 digits.
- each digits used only at once, not like "555", "323"
- if digit "correct" and place "incorrect", counted as "blow".
- if digit "correct" and place "correct", counted as "hit".
% python hitandblow.py
Enter three digits >>123
0 hit 0 blow
Enter three digits >>456
0 hit 1 blow
Enter three digits >>857
1 hit 2 blow
Enter three digits >>875
3 hit 0 blow
congratulation!

2. code sample

import random
 
def judge(c, u):
h=0
b=0
 
for x in range(3):
for y in range(3):
if c[x] == u[y]:
if x == y:
h = h + 1
else:
b = b + 1
 
print("%s hit %s blow" %(h,b))
 
if h == 3:
print("congratulation!")
return True
else:
return False
 
if __name__ == '__main__':
 
com = []
while len(com)!=3:
rannum = random.randint(0,9)
if rannum not in com:
com.append(rannum)
 
while True:
usrnum=input("Enter three digits >>")
usr = (int(usrnum[0]),int(usrnum[1]),int(usrnum[2]))
 
if judge(com,usr) == True:
break

differences between "pyenv", "pyenv-virtualenv" and "virtualenv"

differences between "pyenv", "pyenv-virtualenv" and "virtualenv" are very confusing. clearly defined the definition on this article.

1. pyenv

"pyenv" globally manage python versions. each versions has one environment.
% pyenv versions
  system
  2.7.5
  2.7.6
  2.7.8
* 3.5.0

2. virtualenv

"pyenv-virtualenv" is external function of pyenv. multiple environments can exist on same versions.
~/virtualenv1 % bash bin/activate  # activate peen-virtualenv
(virtualenv1) ~/virtualenv1 % pyenv versions
  system
  2.7.5
  2.7.6
  2.7.8
* 3.5.0 (set by xxx/.python-version)
(virtualenv1) ~/virtualenv1 % deactivate  # deactivate peen-virtualenv
~/virtualenv1 %

3. pyenv-virtualenv

"virtualenv" apply python versions to each directories. virtualenv automatically change environment when user move on the directories.
~/pyenv_test $ cd pyenv-virtualenv1/
(pyenv-virtualenv1) ~/pyenv_test/pyenv-virtualenv1 $

(pyenv-virtualenv1) ~/pyenv_test/pyenv-virtualenv1 $ cat .python-version
pyenv-virtualenv1

how to start ansible?

Ansible is a tool to automatically build and configure virtual machines. There are many ways to build directory tree to execute ansible codes. Today I will show you the easiest way to get start ansible. *on mac os environment

1. install ansible

please check your computer has python. ansible works on python.
% python
...
>>>

if you have not install yet, get it.
*the command below is for mac os.
% brew install python
Then, get ansible.
% pip install ansible

2. prepare directory and files

create the files below under the same directory.
% ls
ansible.cfg  # configuration file
hosts  # hosts file
hello_ansible.yml  # execution file

include define hosts file path and ssh key file path on ansible.cfg file.
% cat ansible.cfg
[defaults]
hostfile = ./hosts
private_key_file=$HOME/.ssh/id_rsa
include target hosts address, username and passwords on ansible.cfg file. I have two hosts "192.168.111.223" and "192.168.111.224".
% cat hosts
[all]
192.168.111.223
192.168.111.224

[server]
192.168.111.223

[client]
192.168.111.224

[all:vars]
ansible_ssh_user=vagrant
ansible_ssh_pass=vagrant
ansible script file is here.
% cat hello_ansible.yml
- hosts: all
  become: true
  user: vagrant
  tasks:
  - debug:
      msg:
        - "hello ansible"

3. execution

% ansible-playbook -i hosts hello_ansible.yml

PLAY [all] *********************************************************************

TASK [setup] *******************************************************************
ok: [192.168.111.223]
ok: [192.168.111.224]

TASK [debug] *******************************************************************
ok: [192.168.111.224] => {
    "msg": [
        "hello ansible"
    ]
}
ok: [192.168.111.223] => {
    "msg": [
        "hello ansible"
    ]
}

PLAY RECAP *********************************************************************
192.168.111.223            : ok=2    changed=0    unreachable=0    failed=0
192.168.111.224            : ok=2    changed=0    unreachable=0    failed=0