用户: dreamable
创建日期: 2022-04-27 18:06:24 UTC
更新日期: 2022-04-27 18:07:36 UTC
引用:(Table ID 44, Record ID 1)

title :
老旦写的战争史
note :

aswetalk 鳕鱼邪恶推荐

标签:
表单: LanguageNotes
用户: dreamable
创建日期: 2022-03-31 09:57:59 UTC
更新日期: 2022-03-31 09:57:59 UTC
引用:(Table ID 41, Record ID 2)

title :
size_t
note :
for(i=0;i<vector.size()-1;++i)

size_t is unsigned integer, so size()-1 is not -1. We should use i+1<vector.size() instead.

标签:
C++
表单: LanguageNotes
用户: dreamable
创建日期: 2021-08-21 08:36:14 UTC
更新日期: 2021-08-21 08:46:23 UTC
引用:(Table ID 41, Record ID 1)

title :
Functors
note :
标签:
表单: LinuxNotes
用户: dreamable
创建日期: 2023-06-28 07:41:42 UTC
更新日期: 2023-06-28 07:41:42 UTC
引用:(Table ID 28, Record ID 12)

标题 :
如何自动重启nginx
笔记 :

服务器nginx偶尔会被杀死,可能是系统资源不够了。

sudo systemctl edit nginx then paste in:

[Service]
Restart=always

check if it works

sudo systemctl status nginx
sudo pkill -f nginx
sudo systemctl status nginx
标签:
表单: LinuxNotes
用户: dreamable
创建日期: 2023-03-10 03:35:42 UTC
更新日期: 2023-03-10 03:43:42 UTC
引用:(Table ID 28, Record ID 11)

标题 :
Install MQTT on Ubuntu 22.04
笔记 :

Install

sudo apt-get install mosquitto mosquitto-clients
sudo mosquitto_passwd -c /etc/mosquitto/passwd <mqtt_user> (set passwd here)
sudo vi /etc/mosquitto/conf.d/default.conf # new file with
  allow_anonymous false # disable anonymous user
  listener 1883 0.0.0.0 # open to all IP, open to localhost by default.
  password_file /etc/mosquitto/passwd # make password work
sudo /etc/init.d/mosquitto restart

Test

mosquitto_sub  -h localhost -u netro -P netrohome -t 'test'
mosquitto_pub -h localhost -u netro -P netrohome -t 'test' -m 'hello'

Increase max open file limit issue

The default max open file is 1024, which may not be enough for large MQTT service, increase it to 65535
# vi /etc/systemd/system.conf
DefaultLimitNOFILE=65536:524288
sudo systemctl daemon-reexec
sudo /etc/init.d/mosquitto restart
# cat /proc/<pid>/limits should be: 
Max open files            65535                524288               files
# count the number of file opened by: 
sudo ls /proc/<pid>/fd | wc -l
标签:
表单: LinuxNotes
用户: dreamable
创建日期: 2022-05-03 23:55:38 UTC
更新日期: 2022-07-07 09:55:10 UTC
引用:(Table ID 28, Record ID 10)

标题 :
LVM 空间跑哪里去了
笔记 :

安装ubuntu,默认LVM,全盘安装。但是装完发现只有100G,另外的130多G去哪里了?

参见guide

扩展

lvextend -l +100%FREE /dev/mapper/ubuntu--vg-ubuntu--lv
resize2fs /dev/mapper/ubuntu--vg-ubuntu--lv
标签:
表单: LinuxNotes
用户: dreamable
创建日期: 2022-04-12 07:25:32 UTC
更新日期: 2022-04-12 07:25:32 UTC
引用:(Table ID 28, Record ID 9)

标题 :
undo remote commits in Git
笔记 :

undo remote commits:

git reset --hard 'xxxxx'

git clean -f -d

git push -f

标签:
表单: LinuxNotes
用户: dreamable
创建日期: 2021-09-08 13:25:30 UTC
更新日期: 2021-09-08 13:25:30 UTC
引用:(Table ID 28, Record ID 8)

标题 :
AWS Elastic IP
笔记 :
  • 保留IP,最多5个
  • 这样EC2重启后IP地址不再变化
  • 有些区域的可以设定reverse DNS,对发送email有帮助
标签:
表单: WebNotes
用户: dreamable
创建日期: 2022-02-09 22:45:22 UTC
更新日期: 2022-02-09 23:02:45 UTC
引用:(Table ID 25, Record ID 7)

标题 :
bootstrap5 updates
笔记 :
    <ul class="navbar-nav me-auto">
      <li class="nav-item">
        <%= link_to t('cloud'), home_index_path, class:"nav-link" %>
      </li>
    </ul>

    <ul class="navbar-nav ms-auto">
      <li class="nav-item">
        <%= link_to t('cloud'), home_index_path, class:"nav-link" %>
      </li>
    </ul>
标签:
表单: WebNotes
用户: dreamable
创建日期: 2021-05-02 22:03:59 UTC
更新日期: 2021-05-02 22:06:07 UTC
引用:(Table ID 25, Record ID 6)

标题 :
bootstrap making dropdown scrollable
笔记 :

guide

  • inline style
style="overflow-y:auto; max-height:80vh"
  • CSS for all
.dropdown-menu {
 overflow-y:auto; 
 max-height:80vh
}
标签:
表单: WebNotes
用户: dreamable
创建日期: 2021-03-05 11:45:34 UTC
更新日期: 2021-03-05 11:45:34 UTC
引用:(Table ID 25, Record ID 5)

标题 :
JS import vs require
笔记 :

guide

  1. require is for CommonJS (node.js way to load modules)
  2. import is for ES6. But not supported via CommonJS by browsers
标签:
表单: WebNotes
用户: dreamable
创建日期: 2021-03-02 11:56:58 UTC
更新日期: 2021-03-02 11:56:58 UTC
引用:(Table ID 25, Record ID 4)

标题 :
Clear form fields with jQuery
笔记 :

guide

$('yourdiv').find('form')[0].reset();
标签:
表单: WebNotes
用户: dreamable
创建日期: 2021-02-25 07:44:00 UTC
更新日期: 2021-02-25 07:44:14 UTC
引用:(Table ID 25, Record ID 3)

标题 :
Find element that has either class 1 or class 2
笔记 :

$(".a, .b")
this will match all elements with class a OR class b

标签:
表单: PythonNotes
用户: dreamable
创建日期: 2023-05-19 09:11:00 UTC
更新日期: 2023-05-24 07:26:00 UTC
引用:(Table ID 14, Record ID 3)

标题 :
目录名改变后venv必须重新初始化
笔记 :

我将engine_py3重新命名为engine后,不工作了!python转为了系统的py2。

必须重新初始化venv环境。例如

python3.8 -m venv env

这是因为,路径被写进去了。

./pyftmerge:#!/home/netro/codes/chao_dev/engine_py3/env/bin/python3.8
./f2py3:#!/home/netro/codes/chao_dev/engine_py3/env/bin/python3.8
./activate.fish:set -gx VIRTUAL_ENV "/home/netro/codes/chao_dev/engine_py3/env"
./activate:VIRTUAL_ENV="/home/netro/codes/chao_dev/engine_py3/env"
./hyper:#!/home/netro/codes/chao_dev/engine_py3/env/bin/python3.8
./ttx:#!/home/netro/codes/chao_dev/engine_py3/env/bin/python3.8
./pyrsa-verify:#!/home/netro/codes/chao_dev/engine_py3/env/bin/python3.8
./futurize:#!/home/netro/codes/chao_dev/engine_py3/env/bin/python3.8
./activate.csh:setenv VIRTUAL_ENV "/home/netro/codes/chao_dev/engine_py3/env"
./pyrsa-sign:#!/home/netro/codes/chao_dev/engine_py3/env/bin/python3.8
./fixup_translate_v3beta1_keywords.py:#!/home/netro/codes/chao_dev/engine_py3/env/bin/python3.8

可见activate中写死了路径。

改变后,找不到了。有些stupid。可以用链接,例如 ln -s engine_py3 engine。可以work

标签:
表单: PythonNotes
用户: dreamable
创建日期: 2021-03-16 01:10:44 UTC
更新日期: 2021-03-16 01:11:23 UTC
引用:(Table ID 14, Record ID 2)

标题 :
Python3 + MongoDB bug
笔记 :

遇到一个奇怪的问题,在Python3中,调用MongoDB。Sorting会影响结果。

基本代码

   order = pymongo.DESCENDING if self.descending else pymongo.ASCENDING
   items = self.tb.find(cond).sort(self.uid,order).limit(self.limit)
   logging.info(f"There are {items.count()} items")
   for idx,t in enumerate(items):
      logging.info(f"Working on {idx}-th item: {t[self.uid]}")

问题:

  1. 当order为descending,limit> # of records, hang after 100 records
  2. 当order为ascending的时候,问题不存在
  3. 当limit <= # of records时候,问题不存在
  4. 只对一个collection有此问题,其它类似collection没有此问题
  5. 存在{uid: -1}的index,添加{uid:1}的index无帮助

找不到原因,可能是某个地方的bug。采用ascending order绕开了这个问题。

标签:
表单: PythonNotes
用户: dreamable
创建日期: 2020-11-23 23:50:21 UTC
更新日期: 2020-12-02 23:47:54 UTC
引用:(Table ID 14, Record ID 1)

标题 :
cython
笔记 :
  1. python vs cpython vs cython

    • python: 语言
    • cpython: Python的解释器,用C实现的,也有用Java实现的Jython
    • cython:优化器,将python编译为C语言,提高运行速度
      • superset language
      • python + C extension
  2. install (version 0.29.21)

    sudo pip install cython
    
  3. setup.py

    from setuptools import setup
    from Cython.Build import cythonize
    setup(
    name='Hello world app',
    ext_modules=cythonize(["hello.py","utils.py","config.py"],annotate=True),
    zip_safe=False,
    )
    
    python setup.py build_ext --inplace
    
    • build error
    • in ~/engine: ERROR: error: [Errno 2] No such file or directory: 'engine/hello.so'
    • in ~/engine/tmp: OK
    • in `~/engine2': OK
    • remove __init__.py solves the problem.
标签:
表单: RailsNotes
用户: dreamable
创建日期: 2023-05-10 02:40:52 UTC
更新日期: 2023-05-10 02:40:52 UTC
引用:(Table ID 3, Record ID 56)

标题 :
Rails Flash.now not working
笔记 :

When redirecting use

flash[:notice] = "This message value is available in next request-response cycle"

When rendering use

flash.now[:notice] = "Message is available in same request-response cycle"

Info from here

标签:
表单: RailsNotes
用户: dreamable
创建日期: 2023-05-10 01:51:37 UTC
更新日期: 2023-05-10 01:51:37 UTC
引用:(Table ID 3, Record ID 55)

标题 :
Rails select with html options.
笔记 :

must put an empty option if want to use html options, e.g. class.

    # class won't work. 
    <%= form.select:status, User.statuses.keys.to_a, class:'form-control col'%> 
    # works
    <%= form.select:status, User.statuses.keys.to_a, {},class:'form-control col'%>
标签:
表单: RailsNotes
用户: dreamable
创建日期: 2023-05-09 22:55:25 UTC
更新日期: 2023-05-10 02:34:52 UTC
引用:(Table ID 3, Record ID 54)

标题 :
Rails7 Hotwire
笔记 :

Rails7有很大的改动。其中影响最大的一个就是Hotwire

  • webpacker被importmap取代
  • Turbolinks被Turbo取代
  • 使用Sprocket进行静态资源管理(和Hotwire无关)

config/application.rb中改为config.load_defaults 7.0才会采用Rails7的默认值。

importmap

可以把webpacker相关的内容全部删除了,新的东西可以在rails7中生成一个新的project,相应的拷贝过来。

app/javascripts只需要application.js,删除其他所有。增加controller目录。注意该目录下需要application.js,否则js报错不能正常工作,我卡在这里好久。

Gemfile中

# Use JavaScript with ESM import maps [https://github.com/rails/importmap-rails]
gem "importmap-rails"
# remove webpacker
gem 'webpacker', '~> 4.0'

Turbo

Gemfile中

# Hotwire's SPA-like page accelerator [https://turbo.hotwired.dev]
gem "turbo-rails"

# remove
# gem 'turbolinks', '~> 5'

# Hotwire's modest JavaScript framework [https://stimulus.hotwired.dev]
gem "stimulus-rails"

需要的改动

Turbo Drive

+ 查询所有turbolinks地方,替换为turbo。如把所有 HTML 的 data 属性的data-turbolinks更新为data-turbo。turbolinks:load -> turbo:load
+ link_to data: :delete|post -> data: {turbo_method: :delete|post}
+ confirm message: data: { confirm: 'Are you sure?' } -> data:{turbo_confirm: 'Are you sure?'}. Together: data: {turbo_method: :delete, turbo_confirm: 'Are you sure?'},
+ method works for button_to

     # link_to works
      <%= link_to t('destroy'), offered_path(@offered), data: {turbo_method: :delete, turbo_confirm: 'Are you sure??'} %>
    # not working, still post method. 
      <%= button_to t('destroy'), offered_path(@offered), data: {turbo_method: :delete, turbo_confirm: 'Are you sure??'} %>
    # works
      <%= button_to t('destroy'), offered_path(@offered), method: :delete, data: {turbo_confirm: 'Are you sure??'} %>
    # confirm not work
      <%= button_to t('destroy'), offered_path(@offered), method: :delete, confirm: 'Are you sure??' %>
  • turobo_confirm doesn't work with link_to alone, check https://stackoverflow.com/a/75046023 ruby # not work <%= link_to t("download"), link, data: { turbo_confirm: 'sure'} %> # works, 注意,如果设置了data-turbo=false,则不work <%= link_to t("download"), link, data: { turbo_method: :get, turbo_confirm:'sure?'}%> # works <%= button_to t("download"), link, data: { turbo_method: :get, turbo_confirm: 'sure?'} %>

Turbo Frame:

+ controller中render需要增加status,否则有Form responses must redirect to another location错误 例如

   format.html { render :signup }  =>
   format.html { render :signup, status: :unprocessable_entity}

+ 出错后render自身页面,需要增加redirect_to

    format.html { flash[:error] = "ERROR" } => 
    format.html { flash[:error] = "ERROR"; redirect_to({action: :login})}
The "default" rails way to do things that I usually see is:

submit a form
redirect on success
render html on error

As such, since Turbo currently requires redirects on all forms, it's a major blocker to prevent folks switching over.

Turbo Steam: 目前没使用

Sprocket

app/assets/config/manifset.js用于配置需要编译的静态内容。典型配置如

/* Sprockets config, static resources to be compiled */
//= link_tree ../images
//= link_directory ../stylesheets .css
//= link_tree ../../javascript .js
//= link_tree ../../../vendor/javascript .js
标签:
表单: RailsNotes
用户: dreamable
创建日期: 2023-05-09 21:56:21 UTC
更新日期: 2023-05-09 21:56:43 UTC
引用:(Table ID 3, Record ID 53)

标题 :
Rails 7 undefined method `javascript_pack_tag'
笔记 :

迁移项目到Rails7,遇到一下问题:

undefined method `javascript_pack_tag'
<%= javascript_pack_tag 'application', 'data-turbo-track': 'reload' %>

根源: javascript_pack_tag is a Webpacker-specific method, so unless you're using Webpacker, you can't use it.

Rails中webpacker被 importmapped Hotwire取代

正确的做法是

    <%= javascript_importmap_tags %>
标签:
表单: RailsNotes
用户: dreamable
创建日期: 2023-02-27 14:40:56 UTC
更新日期: 2023-02-27 14:40:56 UTC
引用:(Table ID 3, Record ID 52)

标题 :
奇怪的Nginx 403错误
笔记 :

Rails开发正常,部署之后403错误,Nginx配置之前用过,没有问题啊。

折腾半天,原来是语法错误,丢失了分号。

  server_name server1.com www.server1.com # 没分号不行
  server_name server1.com www.server1.com; # 有分号就好了
标签: