VS code + parallels + vagrant + ubuntu2204

MacのM1チップではvurtualboxに対応していないため、vagrant + parallelsでubuntu22.04の環境を構築する

% sw_vers
ProductName: macOS
ProductVersion: 13.0
BuildVersion: 22A380

### vagrantインストールおよびubuntuの構築
$ brew install hashicorp/tap/hashicorp-vagrant
$ vagrant -v
$ vagrant plugin install vagrant-parallels
$ vagrant init bento/ubuntu-22.04-arm64

parallelsのsubscriptionを購入する。1年で1万円程度。
https://www.parallels.com/

$ vagrant up –provider=parallels
$ vagrant ssh
$ sudo apt update

### vsコードからの設定
vsコードをインストール
https://azure.microsoft.com/ja-jp/products/visual-studio-code

ExtensionsでRemoteDevelopmentをインストール
次いで、Parallels Desktopもインストールしておく(あまり意味はない)

左メニューのremote exploreでSSHの歯車アイコンから、
Users/${name}/.ssh/configを開く
ここに、vagrant ssh-configの値をそのまま入力する

  HostName 10.211.55.3
  User vagrant
  Port 22
  UserKnownHostsFile /dev/null
  StrictHostKeyChecking no
  PasswordAuthentication no
  IdentityFile /Users/mac/MyVagrant/ubuntu2204/.vagrant/machines/default/parallels/private_key
  IdentitiesOnly yes
  LogLevel FATAL
  PubkeyAcceptedKeyTypes +ssh-rsa
  HostKeyAlgorithms +ssh-rsa

ここで下記の記事のようにhostname, user, port, identityfileのみだとうまく接続出来ないので注意が必要
https://qiita.com/yamashin0616/items/87e042b3f5ec52e57017

後は接続するだけです。Cyberduck、sublimeから変えたいと思っていたので割と簡単に出来て良かった。

【python】マークルツリーのtx1とtx2を組み合わせたhash計算

マークルツリーでsha256のハッシュ計算を行う際に、単純にハッシュ値をつなぎ合わせて、それをさらにsha256で計算しているようだ。
トランザクション1とトランザクション2からのハッシュ値は何度実行しても同じになる。

import hashlib

tx1 = 'abc'
tx2 = 'cde'

tx1hash = hashlib.sha256(tx1.encode()).hexdigest()
print(tx1hash)
tx2hash = hashlib.sha256(tx2.encode()).hexdigest()
print(tx2hash)
tx12 = tx1hash + tx2hash
tx12hash = hashlib.sha256(tx12.encode()).hexdigest()
print(tx12hash)

$ python3 test.py
ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad
08a018a9549220d707e11c5c4fe94d8dd60825f010e71efaa91e5e784f364d7b
e7ea6d8ded8ff13bb4e3fccadb13878004f69bad2d3d9d33f071c13a650303ba

【Python】listのappend

appendとすると、リストの最後に追加される

names = ["Alice", "Bob", "Charlie"]

names.append("Dave")
print(names)

$ python3 test.py
[‘Alice’, ‘Bob’, ‘Charlie’, ‘Dave’]

カッコで囲んで二つ入れると

names = [(“Alice”,”20″), (“Bob”,”23″), (“Charlie”,”25″)]

names.append((“Dave”,”12″))
print(names)
print(names[0][1])
[/code]
基本的に一つしか入らないがタプルとして入れることもできる
$ python3 test.py
[(‘Alice’, ’20’), (‘Bob’, ’23’), (‘Charlie’, ’25’), (‘Dave’, ’12’)]
20

【Python】リストのコロン(:)の使い方: start, end, step

リストのコロンはstart, step, endという意味

for i in range(50)[::10]:
	print(i, end=' ')

$ python3 test.py
0 10 20 30 40

マイナスの場合は前に戻っていく

for i in range(50)[::-10]:
	print(i, end=' ')

$ python3 test.py
49 39 29 19 9

何も指定しない場合は全てのリストが処理される

for i in range(50)[::]:
	print(i, end=' ')

$ python3 test.py
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49

【Python】classの__init__の使い方

pythonでの関数の呼び出し

def some_function(something):
	print(something)

some_function("hello world")

Classにした場合
L インスタンスを生成して処理を実行する
  L クラスの中の関数をメソッドと呼ぶ

class SomeClass:
	def __init__(self, something):
		self.something = something

	def some_function(self):
		print(self.something)


c = SomeClass("hello world")
c.some_function()
class SomeClass:
	def __init__(self, something):
		self.something = something

	def some_function(self):
		print(self.something)

	def some_function2(self):
		return self.something


c = SomeClass("hello world")
c.some_function()
print(c.some_function2())

selfはインスタンス自身のこと
initはデータの初期化の関数

class MyStatus:
	def __init__(self, age, name, height, weight):
		self.age = age
		self.name = name
		self.height = height
		self.weight = weight

	def print_name(self):
		print(self.name)

	def print_age(self):
		print(self.age)

	def print_height(self):
		print(self.height)

	def print_weight(self):
		print(self.weight)

a = MyStatus(14, "yamada", 160, 55)
a.print_height()	 

コーヒーメーカーのClassを作ってみる

class CoffeeMaker:
	def __init__(self, water, brand, tempreture):
		self.water = water # mill
		self.brand = brand
		self.tempreture = tempreture

	def boiling(self):
		boilingTime = self.water / 10 * self.tempreture
		print(boilingTime)

	def brew(self):
		print(self.brand + "を滴れました")

a = CoffeeMaker(300, "brend", 3)
a.boiling()	
a.brew() 

$ python3 test.py
90.0
brendを滴れました

なるほど、Classと関数とinitの使い方について、自分で例を作ると構造を理解できますね。

phpの場合はinitのところが__contructになります。引数はconstructで定義しています。

class Dog {
private $name;

public function __construct($name) {
$this->name = $name;
}

public function getName(){
echo $this->name;
}
}

$dog = new Dog(“Pochi”);

$dog->getName();
[/code]

ini_set('display_errors', "On");

class CoffeeShop {
	private $order;
	private $pay;

	public function __construct($order, $pay) {
		$this->order = $order;
		$this->pay = $pay;
	}

	public function coffeePrice(){
		switch ($this->order) {
			case "ブレンド":
				echo "price is 330 yen\n";
				$price = 330;
				break;
			case "アメリカン":
				echo "price is 350 yen\n";
				$price = 350;
				break;
			case "アイスコーヒー":
				echo "price is 380 yen\n";
				$price = 380;
				break;
			default:
				echo "not available at this store";
		}

		$change = $this->pay - $price;
		return $change;
	}

	
}

$a = new CoffeeShop("ブレンド", 1000);
$b = $a->coffeePrice();
echo $b;

【python】sha256

Pythonのハッシュ値を求める関数はhashlibにある

import hashlib

x = 'abc'

m = hashlib.md5(x.encode()).hexdigest()
sha256 = hashlib.sha256(x.encode()).hexdigest()
sha384 = hashlib.sha384(x.encode()).hexdigest()
sha512 = hashlib.sha512(x.encode()).hexdigest()


print(m)
print(sha256)
print(sha384)
print(sha512)

encodeはコードの符号化
hexdigest は16進数文字列に変換

$ python3 test.py
900150983cd24fb0d6963f7d28e17f72
ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad
cb00753f45a35e8bb5a03d699ac65007272c32ab0eded1631a8b605a43ff5bed8086072ba1e7cc2358baeca134c825a7
ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f

マークルツリーとは

マークルツリーとは?
L 二つのトランザクションのハッシュ値二つを合わせたデータに対してハッシュ値を計算するという操作を繰り返して完成するハッシュ値のツリー構造
一番上のハッシュ値はマークルルートと呼ぶ
ハッシュ値を持つことにより、すべてのトランザクションを保存しなくて済む
ブロックヘッダーのみをwalletは保持する

【sakura vps】alma linuxにgitを入れる

ssh後にgitを確認
$ git –version
-bash: git: command not found

gitが入っていないのでgitを入れる
$ sudo dnf -y install git
Failed to set locale, defaulting to C.UTF-8
Killed

killedとなっているので、logを確認
$ sudo cat /var/log/messages
メモリが足りないっぽいのでswapを追加
$ sudo mkdir /var/swap
$ sudo dd if=/dev/zero of=/var/swap/swap0 bs=1M count=4096
$ sudo chmod 0600 /var/swap/swap0
$ sudo mkswap /var/swap/swap0
$ swapon /var/swap/swap0
$ free
total used free shared buff/cache available
Mem: 469712 94524 143384 15428 231804 336236
Swap: 4194300 40960 4153340

再度git をインストール
$ sudo dnf -y install git
$ git -v
git version 2.39.3