Monday, April 12, 2021

Python3 find string

 


from datetime import datetime


def f1(s,r):
t1 = datetime.utcnow()
if r in s:
t2 = datetime.utcnow()
delta = t2 - t1
print(f'found in {delta.total_seconds()}')
else:
t2 = datetime.utcnow()
delta = t2 - t1
print(f'Not found in {delta.total_seconds()}')


def f2(s,r):
t1 = datetime.utcnow()
idx = s.find(r)
t2 = datetime.utcnow()
delta = t2 - t1
if idx > 0:
print(f'found in {delta.total_seconds()}')
else:
print(f'Not found in {delta.total_seconds()}')


if '__main__' == __name__:
s1 = '1'*10000
s2 = 'abc def ghi jkl mno pqr stu vwx yz'
s3 = '3'*10000
s = f'{s1} {s2} {s3}'
f1(s, 'ghi jkl')
f2(s, 'ghi jkl')


Kết quả cho thấy hàm str.find() tìm kiếm nhanh hơn.


Git, Gitlab command

Using ssh to access repository

- Create a key pair
- Copy/paste the public key content to github.com
- Run the following commands before clone source code
    eval $(ssh-agent)
    ssh-add ~/.ssh/private_key
- Clone source code

Create

  • Create new local repository
    • git init
  • Clone an existing repository
    • gitdone ssh://user@domain.com/repo.git

Local Changes

  • Changed files in your working dir
    • git status
  • Changes to tracked file
    • git diff
  • Add all current changes to next commit
    • git add .
  • Add some changes in <file> to next commit
    • git add -p <file>
  • Commit all local changes in tracked file
    • git commit -a
  • Commit previous staged changes
    • git commit
  • Change the last commit
    • git commit --amend

Commit history

  • Show all commit, starting with newest
    • git log
  • Show changes over time for a specific file
    • git log -p <file>
  • Who change what and when in <file>
    • git blame <file>

Branches and Tags

  • List all existing braches
    • git branch -av
  • Switch HEAD branch
    • git checkout <branch>
  • Create a new branch based on current HEAD
    • git branch <new-branch>
  • Create a new tracking branch based on a remote branch
    • git checkout --track <remote/branch> 
  • Delete a local branch
    • git branch -d <branch>
  • Mark the current commit with a tag
    • git tag <tag-name>

Update & Publish

  • List all currently configured remotes
    • git remote -v
  • Show info about remote
    • git remote show <remote>
  • Add new remote repository, named <remote>
    • git remote add <short name> <url>
  • Download all changes from <remote>, but don't integrate into HEAD
    • git fetch <remote>
  • Download changes and directly merge/integrate into HEAD
    • git pull <remote> <branch>
  • Publish local changes on a remote
    • git push <remote> <branch>
  • Delete a branch on the remote
    • git branch -dr <remote/branch>
  • Publish your tags
    • git push --tags

Merge and rebase

  • Merge branch into current HEAD
    • git merge <branch>
  • Rebase your current HEAD onto <branch>
    • git rebase <branch>
  • Abort a rebase
    • git rebase --abort
  • Continue a rebase after resolving conflicts
    • git rebase --continue
  • Use your configured merge tool to resolve conflicts
    • git mergetool
  • Use your editor to manually resolve conflicts and (alter resolving) mark file as resolved
    • git add <resolved-file>
    • git rm <resolved-file>

Undo

  • Discard all local changes in your working directory
    • git reset --hard HEAD
  • Discard local changes in specific file
    • git checkout HEAD <file>
  • Revert a commit (by producing a new commit with contrary changes)
    • git revert <commit>
  • Reset your HEAD pointer to a previous commit, ... and discard all changes since then
    • git reset --hard <commit>
  • ... and preserve all changes as untagged changes
    • git reset <commit>
  • ... and preserve uncommited local changes
    • git reset --keep <commit>

Add an existing project to Git project

1. Create new repository 
2. Open terminal and change the current working directory to your local project
3. Initialize the local directory as a Git repository
$git init
4. Add the files in your new local repository
$git add .
5. Commit the files that you've staged 
$git commit -m "First commit"
6. Copy remote repository URL
7. Add the URL for remote repository
$git remote add origin <remote_url>
8. Download all change from <remote>, but don't integrate to HEAD
$git fetch origin master:tmp
9. Rebase your current HEAD onto branch
$git rebase tmp
10. Publish local change
$git push origin HEAD:master
11. Delete branch tmp
$git branch -D tmp

Switch to branch

1. View all remote
         $git remote -v
2. Download branches
    2.1 View all branches
         $git branch -r
    2.2.1 Download all branches
         $git fetch --all
    2.2.2 Download a branch
         $git fetch <remote> <branch_name>
3. Switch to branch
         $git checkout -t <remote>/<branch_name> 

Move commits to another branch

Branch A: a1--a2--a3 (2e11c5f) --a4 (2681270) --a5 (08b0f0e)
Branch A: b1--b2--b3--b4--b5
Move commits a3 and a4 to the branch B:
1. Check out the banch A
    $git checkout A
2. Checkout the branch B
    $git checkout B
3. Move the commit a3, a4 to branch B
    $git cherry-pick 2e11c5f
    $git cherry-pick 2681270
4. Reset branch A to last commit
    $git checkout A
    $git reset --hard 08b0f0e

Override tag

Use the -f option to git tag:

-f --force

Replace an existing tag with the given name (instead of failing)
You probably want to use -f in conjunction with -a to force-create an annotated tag instead of a non-annotated one.

Example

Delete the tag on any remote before you push
  $git push origin :refs/tags/<tagname>
Replace the tag to reference the most recent commit
  $git tag -fa <tagname>
Push the tag to the remote origin
  $git push origin master --tags

Checkout pull request

pull request example: want to merge into <organization>:<branch> from <user>:<branch>

You want to checkout from <user>:<branch> but you don't have permission to access <user>:<branch>

1. checkout <organization>:<branch>
2. checkout pull request to a new branch
    $git fetch origin pull/$ID/head:<new_branch_name>
3. checkout <new_branch_name>
    $git checkout <new_branch_name>

When <user> push new commits to pull request, you want to pull these -> do again from step 1.