Posts

Showing posts with the label Python

How to create a "c python"?

In python, all python code are translated into c action in real time. Line by line. If python can do it, c can do it too. In other words, you can create a c_interpreter to translate c code into real time c actions. So that you can create variable in real time, do math operations in real time. import another c file in real time. A example would be: ``` int i = 999; i = i + 1; ``` becomes ``` int *i = (int *)malloc(sizeof(int)); *i = 999; *i = *i + 1; ``` By doing so, you can have a python_like c interpreter, you can run your c code without using gcc again. You can bypass the glibc version restrictions. You can have a python_like interpreter that could handle memory pointer stuff, and speed is just as quick as c. > Sometimes, I think the python is a remote controller, you give it code, it run it line by line to control your computer. Now c could do the same thing.

How to set up a socket service as VPN with pure python?

 # Big direction 1. pure python socket proxy service, one py file 2. ufw allow port 3. use proxy_switch extension in broswer 4. "ssh -L 141:127.0.0.1:141 root@5.3.7.5", connect local 141 as remote 141 # Python script ``` """ Small Socks5 Proxy Server in Python from https://github.com/MisterDaneel/ Modified by yingshaoxo """ # Network import socket import select from struct import pack, unpack # System import traceback from threading import Thread, activeCount from signal import signal, SIGINT, SIGTERM from time import sleep import sys from multiprocessing import Process # # Configuration # MAX_THREADS = 200 BUFSIZE = 2048 TIMEOUT_SOCKET = 5 LOCAL_ADDR = '0.0.0.0' LOCAL_PORT = 2121 # Parameter to bind a socket to a device, using SO_BINDTODEVICE # Only root can set this option # If the name is an empty string or None, the interface is chosen when # a routing decision is made # OUTGOING_INTERFACE = "eth0" OUTGOING_INTERFACE = ...

How to set up jupyter notebook in your server or VPS remotely with subdomain and cloudflare?

 How to set up jupyter notebook in server? # new jupyterlab does not allow cloudflare subdomain access no matter how you try, so we use old pip install jupyterlab==3.2.8 # set up password jupyter lab password # then modify a file 'vim ~/.jupyter/jupyter_server_config.json' to allow subdomain ``` {   "ServerApp": {     "password": "JkZ12Ij",     "allow_origin": "*",     "allow_remote_access": true   } } ~       ``` # new jupyterlab does not have 'jupyter lab' command, so you can only use 'jupyter server'. But it does not allow subdomain access. jupyter lab --ip 127.0.0.1 --port 9998 --allow-root /root/ # I use traefik to do proxy from subdomain to localhost, but use nginx will be easyer since nginx has many tutorial online # auto start, with "crontab -e" @reboot /usr/local/bin/jupyter lab --ip 127.0.0.1 --port 19998 --allow-root /root/

How to make your python code more stable? So it can get running both in python2.7 and python3.x

1. Use zero built-in library or third_party package Try to use built-in functions to do the same thing. If you only focus on one system, that would be easy.   """ abs() all() any() ascii() bin() bool() bytearray() bytes() callable() classmethod() dict() dir() enumerate() eval() exec() float() format() globals() hasattr() hash() help() hex() input() int() isinstance() issubclass() iter() len() list() locals() max() min() object() oct() open() ord() pow() print() property() range() repr() reversed() round() set() setattr() slice() sorted() staticmethod() str() sum() super() tuple() type() vars() zip() __import__() """ 2. Use no types The type will break python2.7, and it will make your code more complex if you use more than json 5 base types. Instead, use this template yingshaoxo created:   ``` Instead of using type in function, you can directly put the type after variable name, so that python2 could run it, for example: def hi(greeting_string):      result...

Stop using third party packages that has many dependencies that the author didn't have a check before they use

33. "remove every backend project that uses node third party packages, they might has backdoor that could use your VPS as their VPS to act like a general web client for hackers"   it is even worse if you use npm in root VPS, because the npm will always use new version of a package or sub_package, how can you make sure your 3000 dependencies will not have a virus that could use your VPS root permission to do bad things?   it is the same for other package manager, for example, python pip. or golang package, java package, rust package, flutter package. in chinese, people usally call those machine that has those software running 'rou ji'. """ 利用 package manager 自动更新新版本的特性,做远控 轻而易举 就算不更新,利用现有的package,二进制文件或者脚本 一个post发到黑客服务器,它就知道你的电脑能不能被控制 对于root设备,被控制就是被完全控制,对于普通设备,就是被当成hacker web 客户端,可以被拿来在网上发垃圾信息、垃圾账号注册 到时候你的ip就会被当成坏ip被服务器墙掉 """

Linux USB midi driver in c or python

C: ``` // Linux MIDI input driver, protocol 1.0, there is no need to upgrade to protocol 2.0 // Author: yingshaoxo // Run it in root shell // gcc main.c -o main.run // ./main.run #include <sys/soundcard.h> #include <fcntl.h> #include <unistd.h> #include <stdio.h> #include <stdlib.h> #define  MIDI_DEVICE  "/dev/midi1" //#define  MIDI_DEVICE  "/dev/dmmidi1" //#define  MIDI_DEVICE  "/dev/snd/midiC1D0" int main(void) {     unsigned char inpacket[4];     // first open the sequencer device for reading.     int seqfd = open(MIDI_DEVICE, O_RDONLY);     if (seqfd == -1) {         printf("Error: cannot open %s\n", MIDI_DEVICE);         exit(1);     }     // now just wait around for MIDI bytes to arrive and print them to screen.     while (1) {         read(seqfd, &inpacket, sizeof(inpacket));    ...

How to draw image directly into a Linux System LCD screen by using frame buffer? (It works in virtualbox)

import os import sys # for exit import mmap # shared memory frame_buffer_number = 'fb0' # device created by the device driver # get width and height f = open(f "/sys/class/graphics/{frame_buffer_number}/virtual_size" , "r" ) width_and_height = f.read() width_string, height_string = width_and_height.split( ',' ) width = int(width_string) # width height = int(height_string) # height f.close # get bits per pixel f = open(f "/sys/class/graphics/{frame_buffer_number}/bits_per_pixel" , "r" ) bpp = int(f.read()) if not bpp in ( 16 , 32 ): print( "Unsupported bpp" ) sys.exit() f.close() # open framebuffer and map it onto a python bytearray frame_buffer_device = open(f "/dev/{frame_buffer_number}" , mode= 'r+b' ) # open R/W frame_buffer_memory_map = mmap.mmap(frame_buffer_device.fileno(), width * height * bpp// 8 , mmap.MAP_SHARED, mmap.PROT_WRITE | mmap.PROT_READ) color = 255 f...

How to download everything under a python HTTP file/folder servering:

 Download everything under a python HTTP servering: In host: ``` python -m http.server ``` In guest: ``` wget -r -np -nH -R / http://192.168.19.22:8000 ```

How to let YouCompleteMe handle c99 project?

Create a .ycm_extra_conf.py file  vim ~/.vim/.ycm_extra_conf.py def Settings( **kwargs ): return { 'flags': [ '-std=gnu99', #'-std=c99', '-x', 'c', #'-std=cpp11', #'-x', 'c++', '-static', '-no-pie', #'-D_POSIX_SOURCE', ], } Add one line in ~/.vimrc vim ~/.vimrc let g:ycm_global_ycm_extra_conf = '~/.vim/.ycm_extra_conf.py' ​

Offline Website Translation in real time, made by yingshaoxo

Javascript:  import * as language_dict from './assets/language_dict.json' ; let is_cn_broswer = ():boolean => { let language = window .navigator.language; if (language.startsWith( "zh-" )) { return true ; } return false ; } let translate_the_page = async () => { // console.log(language_dict) var all = document .getElementsByTagName( "*" ); for ( var i= 0 , max=all.length; i < max; i++) { let one_element = all[i] if (one_element?.textContent) { if (one_element.textContent in language_dict) { one_element.childNodes.forEach((node, index) => { if (node.nodeValue == one_element.textContent) { node.nodeValue = language_dict[one_element.textContent][ 'cn' ] } }) } } } } if (is_cn_broswer()) { translate_the_page() // do the translation...

Sentence Rewrite (Text Formalization) by using machine learning

Method 1  Use ‘Sentence correction’ model to get a correct sentence. https://huggingface.co/shibing624/mengzi-t5-base-chinese-correction Use ‘Punctuation correction’ model to get a more correct sentence. https://huggingface.co/raynardj/classical-chinese-punctuation-guwen-biaodian If you have an advanced AI bot, you may directly ask him/she “Do you know how to speak ‘xxx’ in a another better way?” Method 2 Use ‘Punctuation and grammar correction’ model to get a more correct sentence. https://huggingface.co/oliverguhr/spelling-correction-english-base Use ‘BART Paraphrase’ model to get a simpler sentence. https://huggingface.co/eugenesiow/bart-paraphrase [Optional] Use ‘sentence style transfering’ or ‘text rewrite’ or ‘Text Formalizing’ model to rewrite the sentence. https://huggingface.co/tuner007/pegasus_paraphrase Method 3 Use ‘Offline Google translation’ model to translate that sentence to English language. Use ‘Punctuation and grammar correction’ m...

Yingshaoxo's database design (version 2)

Initiate First, create a folder called database Then, create a yingshaoxo_database_index.txt file under that database folder. For each line, we’ll put a relative_path towards a json_file that under some sub_folders like ./1/current_index_row_number_plus_random_hash_string.json Add  When you add a record, you first read how many lines the yingshaoxo_database_index.txt has, let’s say, index_length = 11 Then you create a json file that has the new data with a name of 11_random_hash_string.json After that, you append that json path at the bottom of yingshaoxo_database_index.txt file. For windows system, it can’t have more than 4,294,967,295 files under a folder. So you’d better seperate files into different folders. Search When you search for a record, you iterate all those json path inside yingshaoxo_database_index.txt . You read each json object. For each json object, you use a python function to handle it. If a record is not what you want, you return None, if a reco...

Yingshaoxo's database design

Initiate First, create a database.txt file. Then, for each line, it is a json string. That json string is actually a dict. Add When you add a record, you append a new json string at the bottom of that database.txt file. Search When you search for a record, you iterate all those json string. For each line of json string, you use a python function to handle it. If a record is not what you want, you return None, if a record is what you want, you return a dict result. In the end, you’ll ignore those None result, and get a list of dict object. >>> import subprocess >>> pycode = """ ... import sys ... if sys.argv[1] == 'nice_guy': ... print('yingshaoxo') ... else: ... print('unrecognized arg') ... """ >>> result = subprocess.run([ 'python' , '-c' , pycode, 'nice_guy' ], stdout=subprocess.PIPE) >>> print(result.stdout.decode()) yingshaoxo Delete I do not r...

How to install tensorflow/pytorch gpu version in ubuntu 20.04 at hp zbook studio g8 laptop

1. Install a special ubuntu system starting from HP website  https://support.hp.com/hk-en/drivers/selfservice/hp-zbook-studio-15.6-inch-g8-mobile-workstation-pc/2100677682 Choose ubuntu, download the iso file, put it into a usb by using Balena Etcher , flush it into your laptop 2. After you get into the ubuntu20.04 system  You could use nvidia-smi to check the GPU memory. 3. Install tensorflow by using anaconda Tensorflow has a tutorial in here: https://www.tensorflow.org/install/pip It’s something like this: conda install -c conda-forge cudatoolkit= 11.8 . 0 python3 -m pip install nvidia-cudnn-cu11== 8.6 . 0.163 tensorflow== 2.12 .* mkdir -p $CONDA_PREFIX /etc/conda/activate.d echo 'CUDNN_PATH=$(dirname $(python -c "import nvidia.cudnn;print(nvidia.cudnn.__file__)"))' >> $CONDA_PREFIX /etc/conda/activate.d/env_vars.sh echo 'export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$CONDA_PREFIX/lib/:$CUDNN_PATH/lib' >> $CONDA_PREFIX /etc/conda/act...