Saturday, March 30, 2019

Python Mac Format

I''m going to show how to used python script to format the string into mac format



#This support both python2 and python3
s=""
h="00233a990c21"
':'.join(h[i:i+2] for i in range(0,12,2))
print ('mac:'+h)
print ('mac format:'+':'.join(h[i:i+2] for i in range(0,12,2)))










Saturday, March 23, 2019

Spilt and Join with datautil

==========Spilt ==============

0)import date fucntion
from datetime import datetime
1) declare varaible

```
string = "2018/01/02" "  " "CCCC"
print string
#'2018/01/02  CCCC'



2) split into two element


```
string.rstrip().split('  ')
#['2018/01/02', 'CCCC']
print string
#2018/01/02  CCCC
```

3) declare varable to element 0 and 1
```
>>> string = "2018/01/02" "  " "CCCC"
>>> element0=string.split()[0]
>>> element1=string.split()[1]
print (element0)
#2018/01/02
print element1
#CCCC
```

 4) convert date format


```
date_convert =(datetime.strftime(datetime.strptime(element0, '%Y/%m/%d'), '%Y%m%d'))
print date_convert 
#20180102

```



5) add two element together


```
new = date_convert +" " +element1
print new
#'20180102 CCCC'

```

 ==========Join/spit==============


spit:
```
>>> a = "this is a string"
>>> a = a.split(" ") # a is converted to a list of strings. 
>>> print a
['this', 'is', 'a', 'string']
```

Joining a string
```
>>> a = "-".join(a)
>>> print a
this-is-a-string 

```



===========datautil with spilt and join full code ==============


 Example 1:

 hoilday.txt

2010/02/15  aaaa
2010/04/15  aaaa    
2010/03/15  aaaa

import datetime as dt
with open('holiday.txt') as f, open('new.txt', 'w') as f_out:
    for line in f:
        line = line.strip()
 line =line.replace('/', '')
 f_out.write('{}\n'.format(line))
 
 

output:
20100315  aaaa



 Example 2:

 hoilday.txt
2010/02/15
import datetime as dt
from datetime import datetime
with open('holiday.txt') as f, open('new.txt', 'w') as f_out:
    for line in f:
        line = line.strip()
 #line =line.replace('/', '')
 #f_out.write('{}\n'.format(line))
 f_out.write(datetime.strftime(datetime.strptime(line, '%Y/%m/%d'), '%Y%m%d'))
output:
20100215


 Example 3:

 hoilday.txt
2010/02/15  aaaa
2010/04/15  aaaa    
2010/03/15  aaaa
import datetime as dt
from datetime import datetime
with open('holiday.txt') as f, open('new.txt', 'w') as f_out:
    for line in f:
        line = line.strip()
 #line =line.replace('/', '')
 #f_out.write('{}\n'.format(line))
 #f_out.write(datetime.strftime(datetime.strptime(line, '%Y/%m/%d'), '%Y%m%d'))
 line.rstrip().split('  ')
 line_1 = line.split()[0]
 line_2 = line.split()[1]
 newline =(datetime.strftime(datetime.strptime(line_1, '%Y/%m/%d'), '%Y%m%d'))
 #result = line_1 +" " +line_2
 #f_out.write(result)
 f_out.write(line_1 +" " +line_2+ '\n')
output:
2010/02/15 aaaa
2010/04/15 aaaa
2010/03/15 aaaa

 Example 4: improvement from example3

 hoilday.txt

2010/02/15  aaaa
2010/04/15  aaaa    
2010/03/15  aaaa
from datetime import datetime
with open('holiday.txt') as f, open('new.txt', 'w') as f_out:
    for line in f:
        line_1, line_2 = line.rstrip().split('  ')
        new_line_1 =(datetime.strftime(datetime.strptime(line_1, '%Y/%m/%d'), '%Y%m%d'))
        f_out.write('{} {}\n'.format(new_line_1, line_2))
output:
2010/02/15 aaaa
2010/04/15 aaaa
2010/03/15 aaaa

dateutil

I would like to share how to used date related library .

First please install datautil related package by pip.


pip install python-dateutil
 Chaged format from XX/XX/XXX to XXXXXX using strftim

from datetime import datetime
d = "2018/01/02"
print(datetime.strftime(datetime.strptime(d, '%Y/%m/%d'), '%Y%m%d'))
Output: 
20180102

from datetime import datetime
d = "2018/01/02"
print(datetime.strftime(datetime.strptime(d, '%Y/%m/%d'), '%Y%m%d
Output: 
Mon Feb 15 10

Input string

Input value using python 2 and python 3
Example1:
```

# -*- coding: utf-8 -*-
#
import os
#python2.X
a = raw_input("enter your name : ")
print "hell0"+a
os.system("pause")
#python3
#a = input("input:")
#print ("name"+ a) 

Finding string from a file

I would like to share how to some string of a file by using python script.It's just like when we are searching some string from a text file.

Purpose of this script:
The text file contains password of day, in other word, meaning everyday contains different password according to different date.
Example 1:
Prerequisite:
You should obtain a text file which contain many data.

Text File:





Source code
```
#python 2
import sys
substring = raw_input("enter your string (Ex:20180810): ")

count =0  
for line in open('POD_RDKB_10Year.txt'):
    #if ("") in line:
    if substring in line:
 line = line.strip()
        count +=1
        print (line)
if count==0:
    print ("string not found")  

```

```
# python3
import sys
substring = input ("enter your string (Ex:20180810):: ")

count =0  
for line in open('POD_RDKB_10Year.txt'):
    #if ("") in line:
    if substring in line:
       line = line.strip()
 
       count +=1
       print (line)
if count==0:
    print ("string not found")

```

  Output
enter your string (Ex:20180810):: 20180810
20180810        eui7pieralWHvt9p


Example2
If you like to used another way, finding keyword written in source code without input it, please refer this source code as below:

```

# -*- coding: utf-8 -*-
#
import sys

string = "Hello Agnosticdev, I love Tutorials"
substring = "Agnosticdev"

# Straight forward approach for Python 2.7 and Python 3.6
# Executes the conditional statement when the substring is found
if substring in string:
 print ("Your substring was found!")



# Alternative Approach

# Use the find method in Python 2.7 or Python 3.6
if string.find(substring) is not -1:
 print("Python found the substring!")
else:
 print("Python did NOT find the substring!")

``` 

Example3

Search your file and export to a new file

```

#support python2 and 3

def find(substr, infile, outfile):
  with open(infile) as a, open(outfile, 'w') as b:
   for line in a:
    if substr in line:
     b.write(line + '\n')
     print(line + '\n')
 
# Example usage:
find('20181109', 'POD_RDKB_10Year.txt', 'b.txt')

``` 

Wednesday, July 13, 2016

GITHUB(simple)


Introduction of using GITHUB.
This are some of the simple setting or tutorial of using github. I will teach you the most simple command.

I will teach you: 
Push file in, add/ delete single folder, add multiply subdirectory.

I had written a slide, please take a took at it: GITHUB_SIMPLE


1)Create a Project (we called this a repositories )
we have to create an github account first before using this.
Go to your GITHUB webpage and create a new repositories .
We can create file on GITHUB page, but i reccomend used a utiity, which I'll introuce next.

2) Utility Introduce
WHY?????
It'll be really convenient when using a utility. You doesn't have to open web browser and upload your file or create folder. Most of the people coding ir creat a project on their own local laptop or PC. We can just sync from our local to the server by typing a few comment, then that's done.

3) Types of utility
There are three type of platfrom on GITHUB, you cn used either of this.

Window 
Window Desktop Source Tree

Linux
debian/ubuntu:  apt-get install git-core
Fedora/Redhat: yum install git-core

MAC: brew install git

NOTE: In my note I'll just used linux as an example,

4) Set up

your username:
#git config --global user.name "YOUR NAME"
your email:
#git config --global user.email "YOUR EMAIL ADDRESS"

your configure
#git config user.name

5)Clone your repository into your local [command: git clone]

sample: git clone  http//github.com/xxxxx/ooooo. 

 X: your username  
0:your repository name
# under Linux utility, create a directory 
mkdir GITHUB 
# clone repository from server to local and download it. Test is repository
git clone  http//github.com/chenchih/test 

6) Commit/Git push (add a new file) add a file in your local disk. Before Git Push we have to commit first. Every time you modify a file we have to commit; commit is to write description on what you modify to let you know

#go to test and create a file
touch 123456.txt
git commit -m "add file "
git push 

7)Add /delete folder[single]

Add folder
#create new folder 
mkdir testfolder 
#also create folder in gitserver 
add 'testfolder' 
#commit it 
git commit -am "Add testfolder" 
git push 
delete Folder
#delete new folder 
rm -rf testfolder 
#also delete folder in gitserver 
git rm -rf 'testfolder' 
#commit it 
git commit -m "Removed folder from repository"
git push 


8)add entire directory

# go into folder directory 
cd testfolder 
git add *
git commit -m "with the message"
git push

9) other command

Fork: when you coworj with other people, you go to their githib, and press fork
diff: is when you modify the file, you can see what's the different between previous

Reference: 
Setting config: 
https://help.github.com/articles/remembering-your-github-username-or-email/
https://help.github.com/articles/set-up-git/

Chinese tutorial 
http://gogojimmy.net/2012/02/29/git-scenario/

Source tree tutoria Chinese
https://superbil.github.io/slide/sourcetree/#sec-1